<?php
class SampleApp extends MemberbaseApp
{
	var $_sample_mod;
	var $_order_mod;
	function __construct()
	{
		$this->SampleApp();
	}
	function SampleApp()
	{
		parent::__construct();
		$this->_sample_mod =& m('sample');
		$this->_order_mod=&m('order');
	}

	function index()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));
		$this->assign("return_url",SITE_URL.$_SERVER['REQUEST_URI']);


		$user_info=$this->visitor->get();
		$this->assign("user",$user_info);

		$this->assign('belongs_sample_customer_reviews', BELONGS_SAMPLE_CUSTOMER_REVIEWS);
		$file_mod =& m('upload');

		$customer_reviews_files = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_CUSTOMER_REVIEWS . ' AND key_id = '.DEFAULT_CUSTOMER_KEY_ID,
				'order' => 'file_id ASC',
		));
		$this->assign('customer_reviews_files', $customer_reviews_files);
		$this->assign('default_customer_key_id', DEFAULT_CUSTOMER_KEY_ID);

		if (isset($_GET['search'])) {
			$sample_sn = empty($_GET['sample_sn']) ? 0 : trim($_GET['sample_sn']);
			$merchandiser = intval($_GET['merchandiser']);
			if($user_info['role']==USER_MERCHANDISER)
			{
				$merchandiser=$user_info['user_id'];
			}
			$buyer = intval($_GET['buyer']);
			$brand_id = intval($_GET['brand_id']);
			$arrival_date_start = strtotime(trim($_GET['arrival_date_start']));
			$arrival_date_end = strtotime(trim($_GET['arrival_date_end']));

			if ($sample_sn) {
				$conditions = " AND sample.sample_sn like '%".$sample_sn."%'";
			} 
			$conditions .= $merchandiser ? ' AND merchandiser_id = ' . $merchandiser : '';
			$conditions .= $buyer ? ' AND sample.buyer_id = ' . $buyer : '';
			$conditions .= $brand_id ? ' AND sample.brand_id = ' . $brand_id : '';
			$conditions .= $arrival_date_start ? ' AND arrival_date >= ' . $arrival_date_start : '';
			$conditions .= $arrival_date_end ? ' AND arrival_date <= ' . $arrival_date_end : '';
			if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
			{
				$mod_company_manager=&m("company_manager");
				$brand_mod =& m('brand');
				$conditions.=" AND sample.brand_id in(select brand_id from {$brand_mod->table} where company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')}))";
			}
			$page = $this->_get_page();
			$cons='status != ' . SAMPLE_STATUS_DELETE . $conditions;
			$sample_list=$this->_sample_mod->get_all_sample_list($cons,'arrival_date DESC, brand_name ASC',$page['limit']);
// 			$sample_list = $this->_sample_mod->find(array(
// 					'conditions' => 'status != ' . SAMPLE_STATUS_DELETE . $conditions,
// 					'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
// 					'order' => 'arrival_date DESC, brand_name ASC, buyer_name ASC',
// 					'count' => true,
// 					'limit' => $page['limit']
// 			));
			$page['item_count'] = $this->_sample_mod->getCount();
			$this->_format_page($page);
			$this->assign('page_info', $page);
			$this->assign('sample_list', $sample_list);

			$this->assign('sample_sn', $sample_sn);
			$this->assign('arrival_date_start', $arrival_date_start);
			$this->assign('arrival_date_end', $arrival_date_end);
		} else {
			//$this->assign('arrival_date_start', gmtime());
			//$this->assign('arrival_date_end', gmtime());
		}

		$this->_assign_user_list(USER_MERCHANDISER, 'merchandiser_list');
		$this->_assign_buyer_list();
		$brand_mod =& m('brand');
// 		if($this->visitor->get("role")==USER_MANAGER)
// 		{
// 			$mod_company_manager=&m("company_manager");
// 			$brand_where=" AND brand.company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')})";
// 		}
		$this->assign('brand_list', $brand_mod->find(array('conditions'=>'if_show = 1'.$brand_where,'order'=>'brand_name','join'=>"belong_company",)));

		$this->display('sample.index.html');

		$this->_curlocal(Lang::get('add_sample'));
	}

	//样板展示
	function reveal(){
		$conditions=$this->_get_query_conditions(array(
			array(
				'field' => 'style_id',
				'equal' => '=',
				'name'	 => 'style_id'
			)
		));
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		$style_mod =& m('style');
		$member_mod=&m('member');
		$member_info=$member_mod->get($user_info['user_id']);
		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
		if ($user_info['role']==USER_CLIENT){
			if ($user_info['is_vip']==1){
				$conditions.=" and brand_id not ".db_create_in(array($member_info['brand_arr_id'])).' and default_image is not null';
			}else{
				$conditions .=" and (is_preset=1 or FIND_IN_SET({$user_info['user_id']},consumer_arr_id)) and default_image is not null";
			}
			$sample_list = $this->_sample_mod->find(array(
					'conditions' => 'status != ' . SAMPLE_STATUS_DELETE . $conditions,
					'count' => true,
					'limit' => $page['limit']
			));
			$file_mod=&m('upload');
			if (!empty($sample_list)){
				foreach ($sample_list as $key=>$rows){
					$buyer_id=$rows['buyer_id'];
					if ($buyer_id==$user_info['user_id']){
						unset($sample_list[$key]);
					}
					$sample_thumb = $file_mod->find(array(
							'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND key_id = ' . $rows['sample_id'],
							'order' => 'file_id ASC',
					));
					if (empty($sample_thumb)){
						unset($sample_list[$key]);
					}
				}
			}
		}
		$page['item_count'] = $this->_sample_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sample_list', $sample_list);
		$this->_curlocal(Lang::get('reveal_sample'));
		$this->display('sample.reveal.html');
	}


	//显示样板详情
	function view_sample(){
		if ($this->visitor->get('role')!=USER_CLIENT){
			$this->show_warning('user_no_acl');
			return;
		}
		$sample_id=empty($_GET['id'])?0:intval($_GET['id']);
		$order_id=empty($_GET['order_id'])?0:intval($_GET['order_id']);
		$sample_handle=empty($_GET['handle'])?'':trim($_GET['handle']);
		if (!$sample_id && !$order_id){
			$this->show_warning('sample_not_exist');
			return;
		}
		if ($sample_handle){
			if ($sample_handle!='edit'){
				$this->show_warning('param_error');
				return;
			}
			$this->assign('sample_handle',$sample_handle);
			if ($sample_id){
				$sample_info=$this->_sample_mod->get(array(
						'conditions'=>'sample_id='.$sample_id
				));
			}elseif ($order_id){
				$sample_info=$this->_order_mod->get(array(
						'conditions'=>'order_id='.$order_id
				));
			}
		}else{
			if ($sample_id){
				$sample_info=$this->_sample_mod->get(array(
						'conditions'=>"sample_id=".$sample_id,
						'fields'	=>'sample_sn,front_pic,other_pic,is_preset'
				));
			}elseif ($order_id){
				$sample_info=$this->_order_mod->get(array(
						'conditions'=>"order_id=".$order_id,
						'fields'	=>'order_sn as sample_sn,front_pic,back_pic as other_pic'
				));
			}
		}
		if (!$sample_info){
			$this->show_warning('sample_not_exist');
			return;
		}
		if (!IS_POST){
			//属于客户的品牌
			$member_mod=&m('member');
			$member_info=$member_mod->get_info($this->visitor->get('user_id'));
			$brand_mod=&m('brand');
			$brands=$brand_mod->find(array(
					'join'		=>'belong_company',
					'conditions'=>'brand_id'.db_create_in($member_info['brand_arr_id'])
			));
			$this->assign('brands',$brands);

			//买手
			$buyer_list=$member_mod->find(array(
					'conditions'=>'user_id='.$this->visitor->get('user_id').' and role='.USER_CLIENT
			));
			$this->assign('buyer_list',$buyer_list);

			//跟单
			if ($member_info['freight_id']){
				$merchandiser_list=$member_mod->find(array(
						'conditions'=>'user_id='.$member_info['freight_id'].' and role='.USER_MERCHANDISER
				));
			}
			$this->assign('merchandiser_list',$merchandiser_list);
			//面料信息
			if ($sample_handle=='edit'){
				$fabric_info_mod =& m('fabric_info');
				if ($sample_id){
					$fabric_info_list = $fabric_info_mod->find(array(
							'conditions'=>'sample_id = ' . $sample_id.' and cc_id<>0',
							'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
							'join'	=>"belongs_fabric_shop",
							'order'=>'fabric_info_id ASC'
					));
				}elseif ($order_id){
					$fabric_info_list = $fabric_info_mod->find(array(
							'conditions'=>'order_id = ' . $order_id.' and cc_id<>0',
							'fields'=>"fabric_info.fabric_info_id,order_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
							'join'	=>"belongs_fabric_shop",
							'order'=>'fabric_info_id ASC'
					));
				}
				
				$this->assign('fabric_info_list', $fabric_info_list);
				//赋值色卡信息
				$colorcard_mode=& m('color_card');
					
				$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");

				$this->assign("fabric_shop_list",$fabric_shop_list);
			}
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));

			//     		$fabricshop_mod =& m('fabric_shop');
			//     		$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));

// 			$factory_mod =& m('factory_info');
// 			$factory_list=$factory_mod->find(array('conditions'=>'factory_id='.DEFAULT_FACTORY_ID,'order'=>'factory_name'));
// 			$this->assign('factory_list', array_filter($factory_list));

			$file_mod =& m('upload');
			if ($sample_handle){
				//细节图
// 				$sample_info['consumer_sample_thumb'] = $file_mod->find(array(
// 						'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND (key_id = ' . $sample_id.' or (session_id="'.session_id().'" and key_id='.DEFAULT_CUSTOMER_KEY_ID.'))',
// 						'order' => 'file_id ASC',
// 				));
				//客户评语附件
				if ($sample_id){
					$sample_info['customer_reviews_upload'] = $file_mod->find(array(
							'conditions' => 'file_type = ' . BELONGS_CONSUMER_REVIEWS . ' AND key_id = ' . $sample_id,
							'order' => 'file_id ASC',
					));
					//客户备注附件
					$sample_info['consumer_remark_upload'] = $file_mod->find(array(
							'conditions' => 'file_type = ' . BELONGS_CONSUMER_REMARK . ' AND key_id = ' . $sample_id,
							'order' => 'file_id ASC',
					));
				}
				
// 				if ($sample_handle=='edit'){
// 					$sample['factory_files'] = $file_mod->find(array(
// 							'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $sample_id,
// 							'order' => 'file_id ASC',
// 					));
// 					$this->assign('belongs_factory', BELONGS_FACTORY);
// 				}
			}
			$this->assign('belongs_consumer_reviews',BELONGS_CONSUMER_REVIEWS);
			$this->assign('belongs_consumer_remark',BELONGS_CONSUMER_REMARK);
			$this->assign('belongs_default_customer',DEFAULT_CUSTOMER_KEY_ID);
// 			$this->assign('belongs_sample_thumb',BELONGS_SAMPLE_THUMB_IMG);
			$this->assign('sample',$sample_info);
			$this->_curlocal(Lang::get('sample_status'),'index.php?app=sample&act=status',Lang::get('sample_info'));
			$this->display('sample.view.html');
		}else{
			$data_arr = array();
			$brand_id = intval($_POST['brand_id']);
			$buyer_id = intval($_POST['buyer_id']);
			$merchandiser_id = intval($_POST['merchandiser_id']);
			$ref_sample_sn = trim($_POST['sample_sn']);
			$front_pic = empty($_POST['front_pic'])?'':trim($_POST['front_pic']);
			$front_pic_small = empty($_POST['front_pic_small'])?'':trim($_POST['front_pic_small']);
			$other_pic = empty($_POST['other_pic'])?'':trim($_POST['other_pic']);
			$style_id = intval($_POST['style_id']);
			$customer_reviews = trim($_POST['customer_reviews']);
			$consumer_remark= trim($_POST['consumer_remark']);
			$order_sn=trim($_POST['order_sn']);
// 			if (!$brand_id) {
// 				$this->show_warning('brand_select_pls');
// 				return;
// 			}
			if (!$buyer_id) {
				$this->show_warning('buyer_select_pls');
				return;
			}
			if (!$merchandiser_id) {
				$this->show_warning('merch_select_pls');
				return;
			}
			//     		if (empty($sample_sn)) {
			//     			$this->show_warning('sample_sn_not_empty');
			//     			return;
			//     		}
			if($sample_handle){
				$s_id=$sample_id;
			}else{
				$data_arr = array(
						'add_time' => gmtime(),
						'brand_id' => $brand_id,
						'buyer_id' => $buyer_id,
						'merchandiser_id' => $merchandiser_id,
						'ref_sample_sn' => $ref_sample_sn,
						'style_id'	=> $style_id,
						'customer_reviews'=>$customer_reviews,
						'consumer_remark'=>$consumer_remark,
						'order_sn' => $order_sn,
						'is_order' => $order_sn ? 1 : 0,
						'front_pic'=>$front_pic,
						'front_pic_small'=>$front_pic_small,
						'other_pic'=>$other_pic
				);
				$s_id = $this->_sample_mod->add($data_arr);
			}
			if (!$s_id) {
				$this->_error($this->_sample_mod->get_error());
			} else {
				$data = array();
				if (!$sample_handle){
					if ($s_id) {
						if (strlen ( $s_id ) <= 6) {
							$k = 6 - strlen ( $s_id );
							for($i = 0; $i < $k; $i ++) {
								$len .= 0;
							}
							$sample_sn = 'YS' . $len . $s_id;
						}
					}
					$data['sample_sn']=$sample_sn;
				}else{
					$data = array(
							'brand_id' => $brand_id,
							'buyer_id' => $buyer_id,
							'merchandiser_id' => $merchandiser_id,
							'style_id'	=> $style_id,
							'customer_reviews'=>$customer_reviews,
							'consumer_remark'=>$consumer_remark,
							'order_sn' => $order_sn,
							'is_order' => $order_sn ? 1 : 0,
							'front_pic'=>$front_pic,
							'front_pic_small'=>$front_pic_small,
							'other_pic'=>$other_pic
					);
				}
				$this->_sample_mod->edit($s_id, $data);
				//处理上传的细节图
				$file_mod =& m('upload');
// 				$sample_thumb_id=isset($_POST['sample_thumb_id'])?$_POST['sample_thumb_id']:"";
// 				if(!empty($sample_thumb_id))
// 				{
// 					$thumb_id=implode(",",$sample_thumb_id);
// 					$cust_thumb_info=$file_mod->find(array(
// 							"conditions"=>"file_id in({$thumb_id}) AND file_type=".BELONGS_SAMPLE_THUMB_IMG." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
// 									));
				
// 					foreach($cust_thumb_info as $item)
// 					{
// 						$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $s_id, $item['file_path']);
// 						if(!file_exists(dirname($file_path))) {
// 							mkdir(dirname($file_path), 0777);
// 						}
// 						rename($item['file_path'], $file_path);
// 						$file_mod->edit("file_id={$item['file_id']}",array(
// 								"file_path"=>$file_path,
// 								"key_id"=>$s_id,
// 								"session_id"=>session_id()
// 						));
// 					}
// 				}
				
				//处理上传的客人评语
				$consumer_reviews_id=isset($_POST['consumer_reviews_id'])?$_POST['consumer_reviews_id']:"";
				$consumer_remark_id=isset($_POST['consumer_remark_id'])?$_POST['consumer_remark_id']:"";
				if(!empty($consumer_reviews_id))
				{
					$file_ids=implode(",",$consumer_reviews_id);
					$cust_files_info=$file_mod->find(array(
							"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_CONSUMER_REVIEWS." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
									));

					foreach($cust_files_info as $item)
					{
						$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $s_id, $item['file_path']);
						if(!file_exists(dirname($file_path))) {
							mkdir(dirname($file_path), 0777);
						}
						rename($item['file_path'], $file_path);
						$file_mod->edit("file_id={$item['file_id']}",array(
								"file_path"=>$file_path,
								"key_id"=>$s_id,
								"session_id"=>session_id()
						));
					}
				}
				if(!empty($consumer_remark_id))
				{
					$remark_ids=implode(",",$consumer_remark_id);
					$remark_files_info=$file_mod->find(array(
							"conditions"=>"file_id in({$remark_ids}) AND file_type=".BELONGS_CONSUMER_REMARK." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
									));

					foreach($remark_files_info as $items)
					{
						$files_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $s_id, $items['file_path']);
						if(!file_exists(dirname($files_path))) {
							mkdir(dirname($files_path), 0777);
						}
						rename($items['file_path'], $files_path);
						$file_mod->edit("file_id={$items['file_id']}",array(
								"file_path"=>$files_path,
								"key_id"=>$s_id,
								"session_id"=>session_id()
						));
					}
				}
			}
			if ($sample_handle){
				$this->show_message('update_sample_base_info_success', 'edit_sample', 'index.php?app=sample&act=status');
			}else{
				$this->show_message('add_sample_success', 'add_sample', 'index.php?app=sample&act=status');
			}
		}
	}


	//客户样板详情页
	function sample_detail(){
		$sample_id=empty($_GET['id'])?0:intval($_GET['id']);
		$user_info=$this->visitor->get();
		if (!$sample_id){
			$this->show_warning('sample_not_exist');
			return;
		}
		$member_mod=&m('member');
		$member_info=$member_mod->get($user_info['user_id']);
		if ($user_info['is_vip']==1){
			$conditions.=" and brand_id not ".db_create_in(array($member_info['brand_arr_id'])).' and sample_id='.$sample_id.' and default_image is not null';
		}else{
			$conditions .=" and (is_preset=1 or FIND_IN_SET({$user_info['user_id']},consumer_arr_id)) and sample_id=".$sample_id." and default_image is not null";
		}
		$sample_info=$this->_sample_mod->get(array(
				'conditions'=>"1=1".$conditions,
				'join'=>'belongs_style'
		));
		if (!$sample_info){
			$this->show_warning('sample_not_exist');
			return;
		}
		//细节图
		$file_mod=& m('upload');
		$sample_info['consumer_sample_thumb'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND key_id = ' . $sample_id,
				'order' => 'file_id ASC',
		));
// 		if ($sample_info['sample_big_pic']){
// 			$big_img=explode(',', $sample_info['sample_big_pic']);
// 			$sample_info['sample_img']=$big_img;
// 		}
		$this->assign('sample',$sample_info);
		$this->_curlocal(Lang::get('reveal_sample'),'index.php?app=sample&act=reveal',Lang::get('detail_sample'));
		$this->display('sample.detail.html');
	}

	
	//客户添加样板
	function add_sample(){
		if (!IS_POST){
			//属于客户的品牌
			$member_mod=&m('member');
			$member_info=$member_mod->get_info($this->visitor->get('user_id'));
			$brand_mod=&m('brand');
			$brands=$brand_mod->find(array(
					'join'		=>'belong_company',
					'conditions'=>'brand_id'.db_create_in($member_info['brand_arr_id'])
			));
			$this->assign('brands',$brands);
			
			//买手
			$buyer_list=$member_mod->find(array(
					'conditions'=>'user_id='.$this->visitor->get('user_id').' and role='.USER_CLIENT
			));
			$this->assign('buyer_list',$buyer_list);
			
			//跟单
			$merchandiser_list=$member_mod->find(array(
					'conditions'=>'user_id='.$member_info['freight_id'].' and role='.USER_MERCHANDISER
			));
			$this->assign('merchandiser_list',$merchandiser_list);
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			
// 			$factory_mod =& m('factory_info');
// 			$factory_list=$factory_mod->find(array('conditions'=>'factory_id='.DEFAULT_FACTORY_ID,'order'=>'factory_name'));
// 			$this->assign('factory_list', array_filter($factory_list));
			
			$this->assign('belongs_consumer_reviews',BELONGS_CONSUMER_REVIEWS);
			$this->assign('belongs_consumer_remark',BELONGS_CONSUMER_REMARK);
			$this->assign('belongs_default_customer',DEFAULT_CUSTOMER_KEY_ID);
			$this->assign('belongs_sample_thumb',BELONGS_SAMPLE_THUMB_IMG);
			$this->_curlocal(Lang::get('reveal_sample'),'index.php?app=sample&act=reveal',Lang::get('add_sample'));
			$this->display('sample.view.html');
		}else{
			$data_arr = array();
			$brand_id = intval($_POST['brand_id']);
			$buyer_id = intval($_POST['buyer_id']);
			$merchandiser_id = intval($_POST['merchandiser_id']);
			$front_pic = trim($_POST['front_pic']);
			$front_pic_small = trim($_POST['front_pic_small']);
			$other_pic = trim($_POST['other_pic']);
			$style_id = intval($_POST['style_id']);
			$customer_reviews = trim($_POST['customer_reviews']);
			$consumer_remark= trim($_POST['consumer_remark']);
			$order_sn=trim($_POST['order_sn']);
// 			if (!$brand_id) {
// 				$this->show_warning('brand_select_pls');
// 				return;
// 			}
			if (!$buyer_id) {
				$this->show_warning('buyer_select_pls');
				return;
			}
			if (!$merchandiser_id) {
				$this->show_warning('merch_select_pls');
				return;
			}
// 			if ($front_pic) {
// 				$data_arr['front_pic'] = $front_pic;
// 				$data_arr['front_pic_small'] = $front_pic_small;
// 			}
// 			if ($other_pic) {
// 				$data_arr['other_pic'] = $other_pic;
// 			}
				$data_arr = array(
					'add_time' => gmtime(),
					'brand_id' => $brand_id,
					'buyer_id' => $buyer_id,
					'merchandiser_id' => $merchandiser_id,
					'style_id'	=> $style_id,
					'customer_reviews'=>$customer_reviews,
					'consumer_remark'=>$consumer_remark,
					//'consumer_price'=>trim($_POST['consumer_price']),
					'order_sn' => $order_sn,
					'is_order' => $order_sn ? 1 : 0
				);
				$s_id = $this->_sample_mod->add($data_arr);
			}
			if (!$s_id) {
				$this->_error($this->_sample_mod->get_error());
			} else {
				$data = array();
				if ($s_id) {
					if (strlen ( $s_id ) <= 6) {
						$k = 6 - strlen ( $s_id );
						for($i = 0; $i < $k; $i ++) {
							$len .= 0;
						}
						$sample_sn = 'YS' . $len . $s_id;
					}
					$data['sample_sn']=$sample_sn;
				}
				//处理上传的图片
				if ($front_pic) {
					$data['front_pic'] = str_replace('temp', $s_id, $front_pic);
					$data['front_pic_small'] = str_replace('temp', $s_id, $front_pic_small);;
					if(!file_exists(dirname($data['front_pic']))) {
						mkdir(dirname($data['front_pic']), 0777);
					}
					rename($front_pic, $data['front_pic']);
				
					if(!file_exists(dirname($data['front_pic_small']))) {
						mkdir(dirname($data['front_pic_small']), 0777);
					}
					rename($front_pic_small, $data['front_pic_small']);
				}
				if ($other_pic) {
					$data['other_pic'] = str_replace('temp', $s_id, $other_pic);
					if(!file_exists(dirname($data['other_pic']))) {
						mkdir(dirname($data['other_pic']), 0777);
					}
					rename($other_pic, $data['other_pic']);
				}
				
				
				$this->_sample_mod->edit($s_id, $data);
				//处理上传的细节图
				$file_mod =& m('upload');
				$sample_thumb_id=isset($_POST['sample_thumb_id'])?$_POST['sample_thumb_id']:"";
				if(!empty($sample_thumb_id))
				{
					$thumb_id=implode(",",$sample_thumb_id);
					$cust_thumb_info=$file_mod->find(array(
							"conditions"=>"file_id in({$thumb_id}) AND file_type=".BELONGS_SAMPLE_THUMB_IMG." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
									));
				
					foreach($cust_thumb_info as $item)
					{
						$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $s_id, $item['file_path']);
						if(!file_exists(dirname($file_path))) {
							mkdir(dirname($file_path), 0777);
						}
						rename($item['file_path'], $file_path);
						$file_mod->edit("file_id={$item['file_id']}",array(
								"file_path"=>$file_path,
								"key_id"=>$s_id,
								"session_id"=>session_id()
						));
					}
				}
				
				//处理上传的客人评语
				$consumer_reviews_id=isset($_POST['consumer_reviews_id'])?$_POST['consumer_reviews_id']:"";
				$consumer_remark_id=isset($_POST['consumer_remark_id'])?$_POST['consumer_remark_id']:"";
				if(!empty($consumer_reviews_id))
				{
					$file_ids=implode(",",$consumer_reviews_id);
					$cust_files_info=$file_mod->find(array(
							"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_CONSUMER_REVIEWS." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
									));
			
					foreach($cust_files_info as $item)
					{
						$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $s_id, $item['file_path']);
						if(!file_exists(dirname($file_path))) {
							mkdir(dirname($file_path), 0777);
						}
						rename($item['file_path'], $file_path);
						$file_mod->edit("file_id={$item['file_id']}",array(
								"file_path"=>$file_path,
								"key_id"=>$s_id,
								"session_id"=>session_id()
						));
					}
				}
				if(!empty($consumer_remark_id))
				{
					$remark_ids=implode(",",$consumer_remark_id);
					$remark_files_info=$file_mod->find(array(
							"conditions"=>"file_id in({$remark_ids}) AND file_type=".BELONGS_CONSUMER_REMARK." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
									));
			
					foreach($remark_files_info as $items)
					{
						$files_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $s_id, $items['file_path']);
						if(!file_exists(dirname($files_path))) {
							mkdir(dirname($files_path), 0777);
						}
						rename($items['file_path'], $files_path);
						$file_mod->edit("file_id={$items['file_id']}",array(
								"file_path"=>$files_path,
								"key_id"=>$s_id,
								"session_id"=>session_id()
						));
					}
				}
			$this->show_message('add_sample_success', 'add_sample', 'index.php?app=sample&act=status');
		}
	}

	//添加样板
	function add()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));

		if (IS_POST) {

			$arrival_date = strtotime(trim($_POST['arrival_date']));
			$brand_id = intval($_POST['brand_id']);
			$buyer_id = intval($_POST['buyer_id']);
			$merchandiser_id = intval($_POST['merchandiser_id']);
			$sample_sn = trim($_POST['sample_sn']);
			$ref_sample_style = intval($_POST['ref_sample_style']);
			$front_pic = trim($_POST['front_pic']);
			$front_pic_small = trim($_POST['front_pic_small']);
			$other_pic = trim($_POST['other_pic']);
			$order_sn = trim($_POST['order_sn']);
			$customer_files_id=isset($_POST['customer_files_id'])?$_POST['customer_files_id']:"";

			if (!$arrival_date) {
				$this->show_warning('arrival_date_not_empty');
				return;
			}
			if (!$brand_id) {
				$this->show_warning('brand_select_pls');
				return;
			}
			if (!$buyer_id) {
				$this->show_warning('buyer_select_pls');
				return;
			}
			if (!$merchandiser_id) {
				$this->show_warning('merch_select_pls');
				return;
			}
			if (empty($sample_sn)) {
				$this->show_warning('sample_sn_not_empty');
				return;
			}
			if (!$ref_sample_style) {
				$this->show_warning('ref_sample_style_select_pls');
				return;
			}

			$data = array(
					'add_time' => gmtime(),
					'arrival_date' => $arrival_date,
					'brand_id' => $brand_id,
					'buyer_id' => $buyer_id,
					'merchandiser_id' => $merchandiser_id,
					'sample_sn' => $sample_sn,
					'ref_sample_style' => $ref_sample_style,
					'order_sn' => $order_sn,
					'is_order' => $order_sn ? 1 : 0
			);

			$sample_id = $this->_sample_mod->add($data);
			if (!$sample_id) {
				$this->_error($this->_sample_mod->get_error());
			} else {
				$data = array();
				/* 处理上传的图片 */
				$data = array();
				if ($front_pic) {
					$data['front_pic'] = str_replace('temp', $sample_id, $front_pic);
					$data['front_pic_small'] = str_replace('temp', $sample_id, $front_pic_small);;
					if(!file_exists(dirname($data['front_pic']))) {
						mkdir(dirname($data['front_pic']), 0777);
					}
					rename($front_pic, $data['front_pic']);

					if(!file_exists(dirname($data['front_pic_small']))) {
						mkdir(dirname($data['front_pic_small']), 0777);
					}
					rename($front_pic_small, $data['front_pic_small']);
				}
				if ($other_pic) {
					$data['other_pic'] = str_replace('temp', $sample_id, $other_pic);
					if(!file_exists(dirname($data['other_pic']))) {
						mkdir(dirname($data['other_pic']), 0777);
					}
					rename($other_pic, $data['other_pic']);
				}

				//处理上传的客人评语
				$file_mod =& m('upload');
				if(!empty($customer_files_id))
				{
					$file_ids=implode(",",$customer_files_id);
					$cust_files_info=$file_mod->find(array(
							"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_SAMPLE_CUSTOMER_REVIEWS." AND key_id=".DEFAULT_CUSTOMER_KEY_ID
					));

					foreach($cust_files_info as $item)
					{
						$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $sample_id, $item['file_path']);
						if(!file_exists(dirname($file_path))) {
							mkdir(dirname($file_path), 0777);
						}
						rename($item['file_path'], $file_path);
						$file_mod->edit("file_id={$item['file_id']}",array(
								"file_path"=>$file_path,
								"key_id"=>$sample_id
						));
					}
				}


				$this->_sample_mod->edit($sample_id, $data);
				/* 处理采购单 */
				$sourcing_mod =& m('sourcing');
				$data = array(
						'type' => SOURCING_LIST_TYPE_SAMPLE,
						'key_id' => $sample_id,
				);
				$sourcing_mod->add($data);
			}
			$this->show_message('add_sample_success', 'add_sample', 'index.php?app=sample');
		}
	}

	function modify()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get_info($id);
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
		if ($sample['status'] != SAMPLE_STATUS_CREATE) {
			$this->show_warning('sample_status_not_create_cant_modify');
			return;
		}

		if (IS_POST) {
			$arrival_date = strtotime(trim($_POST['arrival_date']));
			$brand_id = intval($_POST['brand_id']);
			$buyer_id = intval($_POST['buyer_id']);
			$merchandiser_id = intval($_POST['merchandiser_id']);
			$sample_sn = trim($_POST['sample_sn']);
			$order_sn = trim($_POST['order_sn']);
			$ref_sample_style = intval($_POST['ref_sample_style']);
			$front_pic = trim($_POST['front_pic']);
			$front_pic_small = trim($_POST['front_pic_small']);
			$other_pic = trim($_POST['other_pic']);

			if (!$arrival_date) {
				$this->show_warning('arrival_date_not_empty');
				return;
			}
			if (!$brand_id) {
				$this->show_warning('brand_select_pls');
				return;
			}
			if (!$buyer_id) {
				$this->show_warning('buyer_select_pls');
				return;
			}
			if (!$merchandiser_id) {
				$this->show_warning('merch_select_pls');
				return;
			}
			if (empty($sample_sn)) {
				$this->show_warning('sample_sn_not_empty');
				return;
			}
			if (!$ref_sample_style) {
				$this->show_warning('ref_sample_style_select_pls');
				return;
			}

			$data = array(
					'arrival_date' => $arrival_date,
					'brand_id' => $brand_id,
					'buyer_id' => $buyer_id,
					'merchandiser_id' => $merchandiser_id,
					'sample_sn' => $sample_sn,
					'order_sn' => $order_sn,
					'ref_sample_style' => $ref_sample_style,
			);
			if ($front_pic) {
				$data['front_pic'] = $front_pic;
				$data['front_pic_small'] = $front_pic_small;
			}
			if ($other_pic) {
				$data['other_pic'] = $other_pic;
			}

			$this->_sample_mod->edit($id, $data);
			if ($this->_sample_mod->get_error()) {
				$this->_error($this->_sample_mod->get_error());
			}
			$modify_return_url=isset($_POST['modify_return_url'])?trim($_POST['modify_return_url']):"index.php?app=sample&act=index";

			header("location:{$modify_return_url}");

		}
	}

	function delete()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));
		$user=$this->visitor->get();
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get_info($id);
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		if ($sample['status'] != SAMPLE_STATUS_CREATE) {
			$this->show_message('sample_status_not_in_create_cant_del');
			return;
		}

		$data = array('status' => SAMPLE_STATUS_DELETE);
		$this->_sample_mod->edit($id, $data);
		if ($this->_sample_mod->get_error()) {
			$this->_error($this->_sample_mod->get_error());
			return;
		}
		if ($user['role']==USER_CLIENT){
			$this->show_message('sample_delete_success', 'sample_manage', 'index.php?app=sample&act=status');
		}else{
			$this->show_message('sample_delete_success', 'sample_manage', 'index.php?app=sample');
		}
	}

	function del()
	{
		$this->check_powers(USER_MERCHANDISER);
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sourcing_item_not_exist');
			return;
		}
		$souritem_mod =& m('sourcingitem');
		$souritem = $souritem_mod->get_info($id);
		if (!$souritem) {
			$this->show_warning('sourcing_item_not_exist');
			return;
		}
		if ($souritem['status'] != SOURITEM_STATUS_CREATE) {
			$this->show_warning('sourcing_item_status_not_in_create');
			return;
		}
		$souritem_mod->drop($id);
		if ($souritem_mod->get_error()) {
			$this->_error($souritem_mod->get_error());
			return;
		}
		$this->show_message('delete_sourcing_item_success');
	}

	function stock()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get_info($id);
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		/*if ($sample['status'] != SAMPLE_STATUS_SEND_SAMPLE) {
			$this->show_warning('sample_status_not_send_sample');
		return;
		} else*/
		if (!$sample['confirm_date']) {
			$this->show_warning('sample_not_confirm');
			return;
		} else {
			$data = array('status' => SAMPLE_STATUS_STOCK);
			$this->_sample_mod->edit($id, $data);
			if ($this->_sample_mod->get_error()) {
				$this->_error($this->_sample_mod->get_error());
			}
			$this->show_message('sample_stock_success');
		}
	}

	function re_set()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get_info($id);
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		if ($sample['status'] == SAMPLE_STATUS_CREATE) {
			$this->show_warning('sample_status_is_create');
			return;
		} else {
			/* 样板数据重置 */
			$data = array(
					/*'factory_id' => 0,
					 'to_factory_date' => 0,
			'factory_back_date' => 0,
			'send_sample_date' => 0,
			'factory_price ' => '',
			'sample_made_days' => '',*/
					'is_stock ' => 0,
					'stock_date ' => 0,
					'confirm_date ' => 0,
					'status' => SAMPLE_STATUS_CREATE
			);
			$this->_sample_mod->edit($id, $data);
			if ($this->_sample_mod->get_error()) {
				$this->_error($this->_sample_mod->get_error());
				return;
			}
			/* 采购单数据重置 */
			/*$sourcing_mod =& m('sourcing');
			 $sourcing = $sourcing_mod->get(array(
			 		'conditions' => 'type = ' . SOURCING_LIST_TYPE_SAMPLE . ' AND key_id = ' . $id
			 ));
			$data_sourcing = array(
					'list_sn' => '',
					'sourcing_assistant_id' => 0,
					'to_sourcing_date' => 0,
					'sourcing_accept' => 0,
					'finish_date' => 0,
					'confirm_fabric_date' => 0,
					'sourcing_days' => 0,
			);
			$sourcing_mod->edit($sourcing['list_id'], $data_sourcing);
			if ($sourcing_mod->get_error()) {
			$this->_error($sourcing_mod->get_error());
			return;
			}
			$souritem_mod =& m('sourcingitem');
			$data_souritem = array(
					'status' => SAMPLE_STATUS_CREATE,
			);
			$souritem_mod->edit('list_id = ' . $sourcing['list_id'], $data_souritem);
			if ($souritem_mod->get_error()) {
			$this->_error($souritem_mod->get_error());
			return;
			}*/
			$this->show_message('sample_reset_success');
		}
	}

	function status()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_MERCHANDISER,USER_MERCHANDISER_ADMIN, USER_MANAGER, USER_ASSISTANT));

		$user_info=$this->visitor->get();
		$this->assign("user",$user_info);
		/* 获取品牌 */
		$brand_mod =& m('brand');
		if ($user_info['role']==USER_CLIENT){
			/* 获取排序 */
			$order = empty($_GET['order']) ? 'add_time DESC' : str_replace('-', ' ', trim($_GET['order']));
			if(empty($_GET['order'])){
				$_GET['order']='add_time DESC';
			}
			$sample_sn = empty($_GET['sample_sn']) ? null : trim($_GET['sample_sn']);
			if ($sample_sn) {
				$conditions[] = "sample_sn like '%".$sample_sn."%'";
			}
			$status = !isset($_GET['status']) ? -1 : intval($_GET['status']);
			if ($status!=-1) {
				$conditions[] = "sample.status = $status";
			} else {
				$_GET['status'] = -1;
			}
			$conditions = ($conditions ? ' AND ' : '') . implode(' AND ', $conditions);
			$page = $this->_get_page();
			$con ='sample.status not '.db_create_in(array(SAMPLE_STATUS_DELETE,SAMPLE_STATUS_STOCK)).' and mem.user_id='.$user_info['user_id'].$conditions;
			$sample_list = $this->_sample_mod->get_client_sample_list($con,$order,$page['limit']);
		}else{
			if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
			{
				$mod_company_manager=&m("company_manager");
				$brand_where=" AND brand.company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')})";
			}
			$brand_list = $brand_mod->find(array(
					'conditions' => 'if_show = 1'.$brand_where,
					'join'=>"belong_company",
					'order' => 'brand_name ASC'
			));
			$this->assign('brand_list', $brand_list);

			/* 获取买手 */
			$buyer_mod =& m('member');
			$buyer_list = $buyer_mod->find(array(
					'conditions' => 'if_show = 1 and activation=1 and role='.USER_CLIENT,
					'order' => 'user_name ASC'
			));
			$this->assign('buyer_list', $buyer_list);

			/* 获取采购 */
			$user_mod =& m('member');
			$user_list = $user_mod->find(array(
					'conditions' => 'role = ' . USER_SOURCING . ' AND if_show = 1',
					'order' => 'user_name ASC'
			));
			$this->assign('user_list', $user_list);


			/* 获取工厂 */
			$factory_list = array();
			$arr_info = "";
			$factory_mod =& m('factory_info');
			if($user_info[role]==USER_QC){
				$qf_mod = & m('qcfactory');
				$qf_list = $qf_mod->find(array(
						'fields' => 'factory_id',
						'conditions' => 'user_id='.$user_info['user_id']
				));
				$arr = array();
				if($qf_list){
					foreach($qf_list as $key=>$value){
						$arr[$key] = $value['factory_id'];
					}
					$arr_info = implode(",",$arr);
					if(empty($arr_info)){
						$where = " and factory_id in('') and activation=1";
					}else{
						$where = " and factory_id in(".$arr_info.") and activation=1";
					}
					$factory_list = $factory_mod->find(array(
							'join'=>'belongs_factory_member',
							'conditions' => $where,
							'order' => 'factory_name ASC',
					));
				}
			}elseif ($user_info['role']==USER_SAMPLE){
				$factory_list = $factory_mod->find(array(
						'join'=>'belongs_factory_member',
						'conditions'=>'factory_info.factory_id='.DEFAULT_FACTORY_ID.' and activation=1',
						'order' => 'factory_name ASC',
				));
			}else{
				$factory_list = $factory_mod->find(array(
						'join'=>'belongs_factory_member',
						'order' => 'factory_name ASC',
						'conditions'=>'activation=1'
				));
			}


			$this->assign('factory_list', $factory_list);
			
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));

			/* 获取跟单 */
			$merch_mod =& m('member');
			$merch_list = $merch_mod->find(array(
					'conditions' => 'role = ' . USER_MERCHANDISER . ' AND if_show = 1' . ($this->visitor->get('role') == USER_ASSISTANT ? ' AND user_id in (' . $this->visitor->get('merchandiser') . ')' : ''),
					'order' => 'user_name',
			));
			$this->assign('merch_list', $merch_list);

			/* 获取查询条件 */
			$conditions = array();
			$sample_sn = empty($_GET['sample_sn']) ? null : trim($_GET['sample_sn']);
			if ($sample_sn) {
				$conditions[] = "sample_sn like '%".$sample_sn."%'";
			} 
				$arrival_date_start = empty($_GET['arrival_date_start']) ? 0 : strtotime(trim($_GET['arrival_date_start']));
				if ($arrival_date_start) {
					$conditions[] = "arrival_date >= $arrival_date_start";
				}
				$arrival_date_end = empty($_GET['arrival_date_end']) ? 0 : strtotime(trim($_GET['arrival_date_end']));
				if ($arrival_date_end) {
					$conditions[] = "arrival_date <= $arrival_date_end";
				}
				$brand_id = empty($_GET['brand_id']) ? 0 : intval($_GET['brand_id']);
				if ($brand_id) {
					$conditions[] = "sample.brand_id = $brand_id";
				}
				$buyer_id = empty($_GET['buyer_id']) ? 0 : intval($_GET['buyer_id']);
				if ($buyer_id) {
					$conditions[] = "sample.buyer_id = $buyer_id";
				}
				if ($user_info['role'] != USER_FACTORY)
				{
			  $factory_id = empty($_GET['factory_id']) ? 0 : intval($_GET['factory_id']);
			  if ($factory_id) {
				  $conditions[] = "sample.factory_id = $factory_id";
			  }else{
			  	if($user_info[role]==USER_QC){
			  		if(!empty($arr_info)){
			  			$conditions[] = "sample.factory_id in(".$arr_info.")";
			  		}else{
			  			$conditions[] = "sample.factory_id =0";
			  		}
			  }
			  $status = !isset($_GET['status']) ? -1 : intval($_GET['status']);
			  if ($status != -1) {
				  $conditions[] = "sample.status = $status";
			  } else {
				  $_GET['status'] = -1;
			  }
				
			  //获取款式
			  $style_id=empty($_GET['style_id']) ? 0 : intval($_GET['style_id']);
			  if ($style_id) {
			  	$conditions[] = "sample.style_id = $style_id";
			  }
				}
					
			}
			$user = $this->visitor->get();
			if ($user['role'] == USER_MANAGER) {
				$this->assign('user_manage', USER_MANAGER);
				$merch_id = empty($_GET['merch_id']) ? 0 : intval($_GET['merch_id']);
				if ($merch_id) {
					$conditions[] = "merchandiser_id = $merch_id";
				}
				$conditions[]="1=1";
			} elseif ($user['role'] == USER_ASSISTANT) {
				$merch_id = empty($_GET['merch_id']) ? 0 : intval($_GET['merch_id']);
				$this->assign('user_assistant', USER_ASSISTANT);
				$merch_ids = $user['merchandiser'];
				if ($merch_id) {
					$conditions[] = "merchandiser_id = $merch_id";
				} else {
					$conditions[] = "merchandiser_id in ($merch_ids)";
				}
			}
			elseif ($user['role'] == USER_FACTORY) {
				if (!$user['factory_id']){
					$this->show_warning('没有该工厂,如有疑问,请联系管理员');
					return;
				}
				$merch_id = empty($_GET['merch_id']) ? 0 : intval($_GET['merch_id']);
				if ($merch_id) {
					$conditions[] = "merchandiser_id = $merch_id";
				}
				$this->assign('user_assistant', USER_FACTORY);
				$conditions[] = "sample.factory_id ={$this->visitor->get('factory_id')}";
				$conditions[] = "sample.status in(5,51)";
			}elseif ($user['role']==USER_MERCHANT){
				$conditions[] = "1=1";
			}elseif ($user['role']==USER_MERCHANDISER_ADMIN){
				$this->assign('user_manage',USER_MERCHANDISER_ADMIN);
				$merch_id = empty($_GET['merch_id']) ? 0 : intval($_GET['merch_id']);
				if ($merch_id) {
					$conditions[] = "merchandiser_id = $merch_id";
				}
				$mod_company_manager=&m("company_manager");
				$brand_mod =& m('brand');
				$conditions[]="sample.brand_id in(select brand_id from {$brand_mod->table} where company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')}))";
			}
			else {
				$merch_id = $user['user_id'];
				$conditions[] = "merchandiser_id = $merch_id";
			}
			
			$search_status = !isset($_GET['search_status']) ? "" : trim($_GET['search_status']);
			if ($search_status ) {
				$conditions[] = "sample.status in ($search_status)";
			}
			
			$conditions = ($conditions ? ' AND ' : '') . implode(' AND ', $conditions);


			/* 获取排序 */
			$order = empty($_GET['order']) ? 'add_time DESC' : str_replace('-', ' ', trim($_GET['order']));
			if(empty($_GET['order'])){
				$_GET['order']='add_time DESC';
			}

			if ($order != 'arrival_date DESC' || $order != 'arrival_date ASC') {
				$order .= ', arrival_date DESC';
			}
			$page = $this->_get_page();
			$sample_list = $this->_sample_mod->find(array(
					'fields' => '*, sample.status, sample.description',
					'conditions' => 'ref_sample_style != 3 AND sample.status != ' . SAMPLE_STATUS_STOCK . ' AND sample.status != ' . SAMPLE_STATUS_DELETE . $conditions,
					'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_factory',
					'order' => $order,
					'limit' => $page['limit'],
					'count' => true
			));

			$mod_pro_records=&m("pro_records");

			foreach ($sample_list as $key => $val) {
				$buy_info=$user_mod->get($val['buyer_id']);
				if (!empty($buy_info)){
					$sample_list[$key]['member_name']=$buy_info['user_name'];
				}
				$to_factory_date=!empty($val['to_factory_date'])?intval($val['to_factory_date']):0;
				if ($val['status'] == SAMPLE_STATUS_SEND_SAMPLE || $val['status'] ==SAMPLE_STATUS_STOCK) {
					if (!$val['send_sample_date']) {
						$end_time = time();
					} else {
						$end_time = $val['send_sample_date'];
					}
					if($user['role']==USER_FACTORY)
					{
						$sample_list[$key]['sample_made_days'] = round(($end_time - $to_factory_date) / 60 / 60 / 24);
					}
					else
					{
						$sample_list[$key]['sample_made_days'] = round(($end_time - $val['to_factory_date']) / 60 / 60 / 24);
					}
				}
				else
				{
					if($user['role']==USER_FACTORY)
					{
						$sample_list[$key]['sample_made_days'] = !empty($to_factory_date)?round((time() - $to_factory_date) / 60 / 60 / 24):0;
					}
					else
					{
						$sample_list[$key]['sample_made_days'] =!empty($val['to_factory_date'])? round((time() - $val['to_factory_date']) / 60 / 60 / 24):0;
					}
				}
					
				$sample_list[$key]["is_edit"]=$this->_check_sample_is_edit($val);
				$sample_list[$key]["pro_records"]=$pro_records_list=$mod_pro_records->find(array(
						'conditions'=>"key_id={$val['sample_id']} AND type=".PRO_RECORDS_SAMPLES,
						'fields'=>"rec_time,rec_buff",
						'limit'	=>"0,5",
						'order'	=>"rec_time desc"
								));
			}

			//安照板期排序
		 $order_str=str_replace('-', ' ', trim($_GET['order']));

		 if($order_str=="sample_made_days ASC")
		 {
		  $this->usortSample($sample_list,"asc");
		 }
		 elseif($order_str=="sample_made_days DESC")
		 {
		 	$this->usortSample($sample_list,"desc");

		 }
		}
		$page['item_count'] = $this->_sample_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);

		$this->assign('sample_list', $sample_list);
		$this->display('sample.status.html');
		$this->_curlocal("头板进度");
	}

	function edit_bak()
	{
		/* 检查权限 */
		$this->check_powers(USER_MERCHANDISER);

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = ' . $id,
				'join' => 'belongs_brand,belongs_buyer,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
		if ($sample['status'] == SAMPLE_STATUS_STOCK) {
			header('location:index.php?app=sample&act=show&id=' . $id);
		}
			
		$sourcing_mod =& m('sourcing');
		$sample['sourcing'] = $sourcing_mod->get(array(
				'conditions' => 'type = ' . SOURCING_LIST_TYPE_SAMPLE . ' AND key_id = ' . $sample['sample_id'],
				'join' => 'belongs_sourcing',
		));
		$souritem_mod =& m('sourcingitem');
		$sample['sourcing']['item'] = $souritem_mod->find(array(
				'conditions' => 'list_id = \'' . $sample['sourcing']['list_id'] . "'",
				'join' => 'belongs_fabric_shop',
		));

		if (!IS_POST) {
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));

			/*$special_mod =& m('special');
			 $this->assign('special_list', $special_mod->find(array('conditions'=>'if_show = 1', 'order'=>'special_name ASC')));

			$fabric_mod =& m('fabric');
			$this->assign('fabric_list', $fabric_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_name ASC')));

			$feature_mod =& m('feature');
			$this->assign('feature_list', $feature_mod->find(array('conditions'=>'if_show = 1', 'order'=>'feature_name ASC')));*/

			$fabricshop_mod =& m('fabric_shop');
			$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC', 'limit'=>'20')));

			$factory_mod =& m('factory_info');
			$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
		
			$user_mod =& m('member');
			$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));

			$file_mod =& m('upload');
			$sample['sourcing']['files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_SOURCING_LIST . ' AND key_id = ' . $sample['sourcing']['list_id']
			));
			$sample['factory_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $id
			));

			$this->assign('belongs_sourcing_list', BELONGS_SOURCING_LIST);
			$this->assign('belongs_factory', BELONGS_FACTORY);

			$this->assign('sample', $sample);
			$this->display('sample.form.html');
			$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('more_info'));
		} else {
			if (isset($_POST['base_info'])) {
				if ($sample['confirm_date']) {
					$this->show_warning('sample_confirmed_can_not_update');
					return;
				}
				$style_id = intval($_POST['style_id']);
				/*$fabric_id = intval($_POST['fabric_id']);
				 $feature_id = intval($_POST['feature_id']);
				$special_id = intval($_POST['special_id']);*/
				$front_pic = trim($_POST['front_pic']);
				$other_pic = trim($_POST['other_pic']);

				$data = array(
						'style_id' => $style_id,
						/*'special_id' => $special_id,
						 'fabric_id' => $fabric_id,
				'feature_id' => $feature_id,*/
				);
				if ($front_pic) {
					$data['front_pic'] = $front_pic;
				}
				if ($other_pic) {
					$data['other_pic'] = $other_pic;
				}
				$this->_sample_mod->edit($id, $data);
				if ($this->_sample_mod->get_error())
				{
					$this->_error($this->_sample_mod->get_error());
					return;
				}
				$this->show_message('update_sample_base_info_success', 'sample_info', "index.php?app=sample&act=edit&id=$id");
			} elseif (isset($_POST['sourcing_infom'])) {
				$sourcing_info = trim($_POST['sourcing_info']);
				$fabric_info = trim($_POST['fabric_info']);
				$fabric_shop_id = intval($_POST['fabric_shop_id']);

				if (!$sourcing_info) {
					$this->show_warning('sourcing_info_not_empty');
					return;
				}

				$data = array(
						'sourcing_info' => $sourcing_info,
						'fabric_info' => $fabric_info,
				);
				if ($fabric_shop_id) {
					$data['fabric_shop_id'] = $fabric_shop_id;
				}
				if ($_POST['item_id']) {
					$item_id = intval($_POST['item_id']);
					if (!$item_id) {
						$this->show_warning('sourcing_item_not_exist');
						return;
					}
					$sour_item = $souritem_mod->get_info($item_id);
					if (!$sour_item) {
						$this->show_warning('sourcing_item_not_exist');
						return;
					}
					if ($sour_item['status'] > SOURITEM_STATUS_SOUR_CONFIRM ) {
						unset($data['sourcing_info']);
					}
					if ($sour_item['status'] > SOURITEM_STATUS_MERCH_CONFIRM) {
						$this->show_warning('sourcing_confirmed_cant_modify');
						return;
					}
					$souritem_mod->edit($item_id, $data);
					if ($souritem_mod->get_error()) {
						$this->_error($souritem_mod->get_error());
						return;
					}
					$this->show_message('edit_sourcing_info_success', '', "index.php?app=sample&act=edit&id=$id");
				} else {
					$data['list_id'] = $sample['sourcing']['list_id'];
					if ($sample['sourcing']['list_sn']) {
						$data['status'] = SOURITEM_STATUS_SOUR_CONFIRM;
					}
					$item_id = $souritem_mod->add($data);
					if (!$item_id) {
						$this->_error($souritem_mod->get_error());
						return;
					}
					if ($sample['sourcing']['list_sn']) {
						$sourcing_mod->edit($sample['sourcing']['list_id'], array('status'=>SAMPLE_STATUS_SOUR_CONFIRM));
						$this->_sample_mod->edit($id, array('status'=>SAMPLE_STATUS_SOUR_CONFIRM));
					}
					$this->show_message('add_sourcing_info_success', '', "index.php?app=sample&act=edit&id=$id");
				}
			} elseif (isset($_POST['submit_sourcing'])) {
				if ($sample['status'] > SAMPLE_STATUS_SOUR_FINISH) {
					$this->show_warning('sample_status_is_send_factory');
					return;
				}
				$sourcing_assistant_id = intval($_POST['sourcing_assistant_id']);
				if (!$sourcing_assistant_id) {
					$this->show_warning('sourcing_assistant_select_pls');
					return;
				}
				/*$sourcing_count = count($sourcing_mod->find(array(
					'conditions' => 'confirm_fabric_date = 0 AND list_id != ' . $sample['new_sourcing']['list_id'] . ' AND type = ' . SOURCING_LIST_TYPE_SAMPLE . ' AND key_id = ' . $id
				)));
				if ($sourcing_count) {
				$this->show_warning('can_not_submit_sourcing');
				return;
				}*/
				$souritem = $souritem_mod->find(array(
						'conditions' => 'list_id = ' . $sample['sourcing']['list_id']
				));
				if (!$souritem) {
					$this->show_warning('no_sourcing_item_to_submit');
					return;
				}
				$data = array(
						'list_sn' => $this->create_sourcing_list_sn(),
						'sourcing_assistant_id' => $sourcing_assistant_id,
						'to_sourcing_date' => time(),
						'status' => SAMPLE_STATUS_SOUR_CONFIRM
				);
				$sourcing_mod->edit($sample['sourcing']['list_id'], $data);
				$this->_sample_mod->edit($id, array('status'=>SAMPLE_STATUS_SOUR_CONFIRM));
				if ($sourcing_mod->get_error()) {
					$this->_error($sourcing_mod->get_error());
					return;
				}
				$souritem_mod->edit('list_id = ' . $sample['sourcing']['list_id'] . ' AND status < ' . SOURITEM_STATUS_SOUR_CONFIRM, array('status'=>SOURITEM_STATUS_SOUR_CONFIRM));
				//$sourcing_mod->add(array('key_id'=>$id,'type'=>SOURCING_LIST_TYPE_SAMPLE));
				$this->show_message('submit_sourcing_success', 'sample_status', 'index.php?app=sample&act=edit&id='.$id);
			} elseif (isset($_POST['assign_factory'])) {
				/*if ($sample['status'] != SAMPLE_STATUS_SOUR_FINISH) {
					$this->show_warning('sample_status_not_in_sour_finish');
				return;
				}*/
				$factory_id = intval($_POST['factory_id']);
				$to_factory_date = strtotime(trim($_POST['to_factory_date']));

				if (!$factory_id) {
					$this->show_warning('factory_select_pls');
					return;
				}
				if (!$to_factory_date) {
					$this->show_warning('to_factory_date_select_pls');
					return;
				}

				$data = array(
						'factory_id' => $factory_id,
						'to_factory_date' => $to_factory_date,
						'status' => SAMPLE_STATUS_SEND_FACTORY
				);
				$this->_sample_mod->edit($id, $data);
				if ($this->_sample_mod->get_error()) {
					$this->_error($this->_sample_mod->get_error());
					return;
				}
				$this->show_message('sample_assign_factory_success', 'sample_stutes', 'index.php?app=sample&act=edit&id=' . $id);
			} elseif (isset($_POST['update_sample'])) {
				if ($sample['confirm_date']) {
					$this->show_warning('sample_confirmed_can_not_update');
					return;
				}
				if (!$sample['factory_back_date']) {
					$factory_back_date = empty($_POST['factory_back_date']) ? 0 : strtotime(trim($_POST['factory_back_date']));
				}
				if (!$sample['send_sample_date']) {
					$send_sample_date = empty($_POST['send_sample_date']) ? 0 : strtotime(trim($_POST['send_sample_date']));
				}
				//$factory_back_date = empty($_POST['factory_back_date']) ? 0 : strtotime(trim($_POST['factory_back_date']));
				$factory_price = trim($_POST['factory_price']);
				//$send_sample_date = empty($_POST['send_sample_date']) ? 0 : strtotime(trim($_POST['send_sample_date']));
				$description = trim($_POST['description']);

				if ($factory_back_date && $factory_back_date < $sample['to_factory_date']) {
					$this->show_warning('factory_back_date_error');
					return;
				}
				if ($send_sample_date && $send_sample_date < $factory_back_date) {
					$this->show_warning('send_sample_date_error');
					return;
				}

				$data = array(
						//'factory_back_date' => $factory_back_date,
						'factory_price' => $factory_price,
						//'send_sample_date' => $send_sample_date ? $send_sample_date : null,
						'description' => $description
				);
				if ($factory_back_date) {
					$data['factory_back_date'] = $factory_back_date;
					$data['sample_made_days'] = intval(($factory_back_date - $sample['to_factory_date']) / 60 / 60 / 24) + 1;
				}
				if ($send_sample_date) {
					$data['send_sample_date'] = $send_sample_date;
				}

				if ($sample['status'] == SAMPLE_STATUS_SEND_FACTORY && $send_sample_date) {
					$data['status'] = SAMPLE_STATUS_SEND_SAMPLE;
				}

				$this->_sample_mod->edit($id, $data);
				if ($this->_sample_mod->get_error()) {
					$this->_error($this->_sample_mod->get_error());
					return;
				}
				$this->show_message('update_sample_factory_success', 'sample_stutes', 'index.php?app=sample&act=edit&id=' . $id);
			}
		}
	}

	function show()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$user_info=$this->visitor->get();

		//赋值用户信息
		$this->assign("user",$user_info);

		//跟单可以保存备注
		if(IS_POST && in_array($user_info['role'], array(USER_MERCHANDISER,USER_CIVILIAN)))
		{
			$customer_reviews_text = trim($_POST['customer_reviews_text']);
			$this->_sample_mod->edit($id,array('customer_reviews_text'=>$customer_reviews_text));
			if ($this->_sample_mod->get_error())
			{
				$this->_error($this->_sample_mod->get_error());
				return;
			}
		}

		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = ' . $id,
				'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		if($this->visitor->get("role")==USER_FACTORY&&$sample['factory_id']!=$this->visitor->get("factory_id"))
		{
			$this->show_warning('用户无权限查看此样板');
			return;
		}
		$this->_assign_next_sample($id);

		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>'sample_id = ' . $id,
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));

		$this->assign('fabric_info_list', $fabric_info_list);
			
		//赋值进度记录
		$mod_pro_records=&m("pro_records");
		$pro_records_list=$mod_pro_records->find(array(
				'conditions'=>"key_id={$id} AND type=".PRO_RECORDS_SAMPLES,
				'order'	=>"rec_time desc"
						));
		$this->assign("pro_records_list",$pro_records_list);
			
		//赋值色卡信息
		$colorcard_mode=& m('color_card');
			
		$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");

		$this->assign("fabric_shop_list",$fabric_shop_list);
			
		$style_mod =& m('style');
		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			
		$fabricshop_mod =& m('fabric_shop');
		$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
			
		$factory_mod =& m('factory_info');
		$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
			
		$user_mod =& m('member');
		if ($sample['buyer_id']){
			$buyers=$user_mod->get(array(
				'fields'=>'user_name',
				'conditions'=>'user_id='.$sample['buyer_id'].' and role='.USER_CLIENT
			));
			$sample['buyer_name']=$buyers['user_name'];
		}
		$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));

		$file_mod =& m('upload');
		//客户上传的附件start
		$sample['customer_reviews_upload'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_CONSUMER_REVIEWS . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
		$sample['consumer_remark_upload'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_CONSUMER_REMARK . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
		//end
		$sample['colorcard_files'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_COLORCARD . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
		$sample['factory_files'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
			
		$sample['file_size'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_FILESIZE . ' AND key_id = ' . $id
		));
			
		$sample['customer_reviews_files'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_CUSTOMER_REVIEWS . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));

		$sample['makepanel_files'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_MAKEPANEL . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));


		$brand_mod =& m('brand');
		$company_mod=& m('company');
		$brand_info=$brand_mod->get($sample['brand_id']);
		$company_info=$company_mod->get(array(
			'conditions'=>'company_id='.$brand_info['company_id']
		));

		$sample['company_info'] = $company_info;

		//细节图
		$sample['consumer_sample_thumb'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
		$this->assign('belongs_sample_thumb',BELONGS_SAMPLE_THUMB_IMG);
		$this->assign('belongs_colorcard', BELONGS_COLORCARD);
		$this->assign('belongs_factory', BELONGS_FACTORY);
		$this->assign('belongs_sample_filesize', BELONGS_SAMPLE_FILESIZE);
		$this->assign('belongs_consumer_reviews',BELONGS_CONSUMER_REVIEWS);
		$this->assign('belongs_consumer_remark',BELONGS_CONSUMER_REMARK);
		$this->assign('belongs_sample_customer_reviews', BELONGS_SAMPLE_CUSTOMER_REVIEWS);
		$this->assign('sample', $sample);
		$this->display('sample.show.html');
		$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('more_info'));

	}

	function CountSourcingTask()
	{
		$user_mod =& m('member');
		$users = $user_mod->find(array(
				'fields' => 'user_name, user_id',
				'conditions' => 'role = ' . USER_SOURCING,
				'order' => 'user_name ASC'
		));
		$sourcing_mod =& m('sourcing');
		foreach ($users as $key => $user) {
			$users[$key]['count'] = count($sourcing_mod->find(array(
					'conditions' => 'sourcing_assistant_id = ' . $user['user_id'] . ' AND status > ' . SAMPLE_STATUS_SOUR_CONFIRM . ' AND status < ' . SAMPLE_STATUS_SOUR_FINISH,
			)));
		}
		/* 按未完成采购任务数排序 */
		$temp = array();
		foreach ($users as $key => $user) {
			foreach ($users as $k => $v) {
				if ($v['count'] > $user['count']) {
					$temp = $v;
					$users[$key] = $user;
					$users[$k] = $temp;
				}
			}
		}
		$this->assign('task', $users);
		$this->display('sample.task.html');
	}

	function confirm()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			echo ecm_json_encode(array('msg'=>'样板不存在！'));
			return;
		}
		$sample = $this->_sample_mod->get_info($id);
		if (!$sample) {
			echo ecm_json_encode(array('msg'=>'样板不存在！'));
			return;
		}
		if ($sample['status'] < SAMPLE_STATUS_SEND_SAMPLE) {
			echo ecm_json_encode(array('msg'=>'样板未处于“已寄板”状态，不能确认！'));
			return;
		}
		$data = array(
				'confirm_date' => time()
		);
		$this->_sample_mod->edit('sample_id = ' . $id, $data);
		if ($this->_sample_mod->get_error()) {
			echo ecm_json_encode(array('msg'=>$this->_sample_mod->get_error()));
			return;
		}
		echo ecm_json_encode(array('msg'=>'确认样板成功！'));
		/*$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		 if (!$id) {
		$this->show_warning('sourcing_item_not_exist');
		return;
		}

		$souritem_mod =& m('sourcingitem');
		$souritem = $souritem_mod->get(array(
				'fields' => '*, sourcing_item.status',
				'conditions' => 'item_id = ' . $id,
				'join' => 'belongs_sourcing',
		));
		if (!$souritem) {
		$this->show_warning('sourcing_item_not_exist');
		return;
		}
		if ($souritem['status'] != SOURITEM_STATUS_MERCH_CONFIRM) {
		$this->show_warning('sourcing_not_finish');
		return;
		}
		$souritem_mod->edit($id, array('status'=>SOURITEM_STATUS_CONFIRMED));
		if ($souritem_mod->get_error()) {
		$this->_error($souritem_mod->get_error);
		return;
		}
		$not_confirms = count($souritem_mod->find(array(
				'conditions' => 'status < ' . SOURITEM_STATUS_CONFIRMED . ' AND list_id = ' . $souritem['list_id'],
		)));
		if ($souritem['type'] == SOURCING_LIST_TYPE_SAMPLE && !$not_confirms) {
		$time = time();
		$data = array(
				'confirm_fabric_date' => $time,
				'sourcing_days' => intval(($time - $souritem['sourcing_accept']) / 60 / 60 / 24) + 1,
				'status' => SAMPLE_STATUS_SOUR_FINISH
		);
		$sourcing_mod =& m('sourcing');
		$sourcing_mod->edit($souritem['list_id'], $data);
		//$sourcing = $sourcing_mod->get_info($souritem['key_id']);
		//$sample = $this->_sample_mod->get_info($sourcing['key_id']);
		$this->_sample_mod->edit($souritem['key_id'], array('status'=>SAMPLE_STATUS_SOUR_FINISH));
		}
		if ($souritem['type'] == SOURCING_LIST_TYPE_SAMPLE) {
		header('location:index.php?app=sample&act=edit&id=' . $souritem['key_id']);
		}*/
	}

	function confirm_sample()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}

		$sample = $this->_sample_mod->get_info($id);
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		/*if ($sample['status'] != SAMPLE_STATUS_SEND_SAMPLE) {
			$this->show_warning('sample_status_not_in_send_sample');
		return;
		}*/

		if ($sample['confirm_date']) {
			$this->show_warning('sample_confirmed');
			return;
		}

		$this->_sample_mod->edit($id, array('confirm_date'=>time()));
		if ($this->_sample_mod->get_error()) {
			$this->_error($this->_sample_mod->get_error());
			return;
		}
		$this->show_message('confirm_sample_success', Lang::get('sample_status'), 'index.php?app=sample&act=status');
	}



	function search()
	{
		$user_info=$this->visitor->get();
		$this->assign("user",$user_info);
		$brand_mod=&m('brand');
		$merch_mod =& m('member');
		if($user_info['role']==USER_CLIENT){
			//属于客户的品牌
			$member_mod=&m('member');
			$member_info=$member_mod->get_info($this->visitor->get('user_id'));
			$brands=$brand_mod->find(array(
					'join'		=>'belong_company',
					'conditions'=>'brand_id'.db_create_in($member_info['brand_arr_id'])
			));
			$this->assign('brand_list',$brands);

			//买手
			$buyer_list=$member_mod->find(array(
					'conditions'=>'user_id='.$this->visitor->get('user_id').' and role='.USER_CLIENT
			));
			$this->assign('buyer_list',$buyer_list);

			//跟单
			$merchandiser_list=$member_mod->find(array(
					'conditions'=>'user_id='.$member_info['freight_id'].' and role='.USER_MERCHANDISER
			));
			$this->assign('merchandiser_list',$merchandiser_list);

			/* 获取查询条件 */
			$conditions = array();
			$sample_sn = empty($_GET['sample_sn']) ? null : trim($_GET['sample_sn']);
			if ($sample_sn) {
				$conditions[] = "sample_sn like '%".$sample_sn."%'";
			}
			$brand_id = empty($_GET['brand_id']) ? 0 : intval($_GET['brand_id']);
			if ($brand_id) {
				$conditions[] = "sample.brand_id = $brand_id";
			}
			$conditions[] = "sample.buyer_id = ".$user_info['user_id'];
			$status = !isset($_GET['status']) ? -1 : intval($_GET['status']);
			if ($status != -1) {
				$conditions[] = "sample.status = $status";
			} else {
				$_GET['status'] = -1;
			}
			$conditions = ($conditions ? ' AND ' : '') . implode(' AND ', $conditions);
			$page = $this->_get_page();
			$order = empty($_GET['order']) ? 'arrival_date DESC' : str_replace('-', ' ', trim($_GET['order']));
			if ($order != 'arrival_date DESC' || $order != 'arrival_date ASC') {
				$order .= ', arrival_date DESC';
			}
			$con='sample.status not '.db_create_in(array(SAMPLE_STATUS_DELETE)).' and mem.user_id='.$user_info['user_id'].$conditions;
			$sample_list = $this->_sample_mod->get_client_sample_list($con,$order,$page['limit']);
		}else{
			$sample_sn = empty($_GET['sample_sn']) ? '' : trim($_GET['sample_sn']);
			$order_sn = empty($_GET['order_sn']) ? '' : trim($_GET['order_sn']);
			$page = $this->_get_page(20);
			$factory_item = -1;

			$status = !isset($_GET['status']) ? -1 : intval($_GET['status']);
			if ($status == -1) {
				$_GET['status'] = -1;
			}

			if($user_info[role]==USER_QC){
				$qf_mod = & m('qcfactory');
				$qf_list = $qf_mod->find(array(
						'fields' => 'factory_id',
						'conditions' => 'user_id='.$user_info['user_id']
				));
				$arr_info = "";
				if($qf_list){
					$arr = array();
					foreach($qf_list as $key=>$value){
						$arr[$key] = $value['factory_id'];
					}
					$arr_info = implode(",",$arr);
				}
			}

			$this->assign('factory_item', $factory_item);
			if ($sample_sn) {
				$con="";
				if($user_info['role']==USER_FACTORY)
				{
					if (!$user_info['factory_id']){
						$this->show_warning('没有该工厂,如有疑问,请联系管理员');
						return;
					}
					$con=" AND sample.status in(5,51,6,7) AND sample.factory_id ={$this->visitor->get('factory_id')}";
				}

				if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
				{
					//$mod_company_manager=&m("company_manager");
					//$con.=" and sample.brand_id in(select brand_id from {$brand_mod->table} where company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')}))";
				}

				$sample_list = $this->_sample_mod->find(array(
						'conditions' => "sample_sn like '%".$sample_sn."%'".$con.' and sample.status !='.SAMPLE_STATUS_DELETE,
						'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_fabric,belongs_feature,belongs_special,belongs_factory',
						'order' => 'arrival_date DESC, brand_name ASC, user_name ASC'
				));
				$this->assign('sample_sn', $sample_sn);
			} elseif ($order_sn) {
				$con="";
				if($user_info['role']==USER_FACTORY)
				{
					if (!$user_info['factory_id']){
						$this->show_warning('没有该工厂,如有疑问,请联系管理员');
						return;
					}
					$con=" AND sample.status in(5,51,6,7) AND sample.factory_id ={$this->visitor->get('factory_id')}";
				}

				if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
				{
					//$mod_company_manager=&m("company_manager");
					//$con.=" and sample.brand_id in(select brand_id from {$brand_mod->table} where company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')}))";
				}

				$sample_list = $this->_sample_mod->find(array(
						'conditions' => "order_sn = '$order_sn'".$con.' and sample.status!='.SAMPLE_STATUS_DELETE,
						'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_fabric,belongs_feature,belongs_special,belongs_factory',
						'order' => 'arrival_date DESC, brand_name ASC, user_name ASC'
				));
				$this->assign('order_sn', $order_sn);
			} else{
				$conditions = '1=1';
				$arrival_date_start = empty($_GET['arrival_date_start']) ? 0 : strtotime(trim($_GET['arrival_date_start']));
				$arrival_date_end = empty($_GET['arrival_date_end']) ? 0 : strtotime(trim($_GET['arrival_date_end']));
				if ($arrival_date_start) {
					$conditions .= ' AND arrival_date >= ' . $arrival_date_start;
					$this->assign('arrival_date_start', $arrival_date_start);
				}
				if ($arrival_date_end) {
					$conditions .= ($conditions ? ' AND ' : '') . 'arrival_date <= ' . $arrival_date_end;
					$this->assign('arrival_date_end', $arrival_date_end);
				}
				$brand_id = empty($_GET['brand_id']) ? 0 : intval($_GET['brand_id']);
				if ($brand_id) {
					$conditions .= ($conditions ? ' AND ' : '') . 'sample.brand_id = ' . $brand_id;
					$this->assign('brand_id', $brand_id);
				}
				$buyer_id = empty($_GET['buyer_id']) ? 0 : intval($_GET['buyer_id']);
				if ($buyer_id) {
					$conditions .= ($conditions ? ' AND ' : '') .  'sample.buyer_id = ' . $buyer_id;
					$this->assign('buyer_id', $buyer_id);
				}
				$merch_id = empty($_GET['merchandiser_id']) ? 0 : intval($_GET[merchandiser_id]);
				if ($merch_id) {
					$conditions .= ($conditions ? ' AND ' : '') . 'merchandiser_id = ' . $merch_id;
					$this->assign('merch_id', $merch_id);
				}
				$ref_sample_style = empty($_GET['ref_sample_style']) ? 0 : intval($_GET['ref_sample_style']);
				if ($ref_sample_style) {
					$conditions .= ($conditions ? ' AND ' : '') .  'ref_sample_style = ' . $ref_sample_style;
					$this->assign('ref_sample_style', $ref_sample_style);
				}
				$style_id = empty($_GET['style_id']) ? 0 : intval($_GET['style_id']);
				if ($style_id) {
					$conditions .= ($conditions ? ' AND ' : '') .  'sample.style_id = ' . $style_id;
					$this->assign('style_id', $style_id);
				}

				if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
				{
					//$mod_company_manager=&m("company_manager");
					//$conditions.=" AND sample.brand_id in(select brand_id from {$brand_mod->table} where company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')}))";
				}

				if ($this->visitor->get("role")==USER_MANAGER || ($this->visitor->get("role")==USER_MERCHANDISER && $this->visitor->get('role_cate')==ROLE_MERCH)){
					if (!empty($_GET['assign_buyer'])){
						$conditions .= ($conditions ? ' AND ' : '') .  'FIND_IN_SET('.$_GET['assign_buyer'].',consumer_arr_id)';
						$this->assign('cancel_user',$_GET['assign_buyer']);
					}
				}
				
				$factory_id = empty($_GET['factory_id']) ? 0 : intval($_GET['factory_id']);
				if ($factory_id) {
					$conditions .= ($conditions ? ' AND ' : '') .  'sample.factory_id = ' . $factory_id;
					$this->assign('factory_id', $factory_id);
				}else{
					if($user_info[role]==USER_QC){
// 						if(!empty($arr_info)){
// 							$conditions .= ($conditions ? ' AND ' : '') .  'sample.factory_id in('.$arr_info.') ';
// 						}else{
// 							$conditions .= ($conditions ? ' AND ' : '') .  "sample.factory_id = 0 ";
// 						}  
					}
				}

				if ($user_info['role'] == USER_FACTORY) {
					if (!$user_info['factory_id']){
						$this->show_warning('没有该工厂,如有疑问,请联系管理员');
						return;
					}
					$this->assign('user_assistant', USER_FACTORY);
					$conditions .= ($conditions ? ' AND ' : '') .  "sample.factory_id ={$this->visitor->get('factory_id')} AND sample.status in(5,51,6,7)";
				}

				if ($status != -1) {
					$conditions .= ($conditions ? ' AND ' : '') .  "sample.status ={$status}";
				}

				$fabric_shop_id = empty($_GET['fabric_shop_id']) ? 0 : intval($_GET['fabric_shop_id']);
				$factory_item =isset($_GET['factory_item'])? trim($_GET['factory_item']):-1;
				$fi_mod =& m('fabric_info');
				if ($fabric_shop_id && $factory_item!=-1) {
					$fi = $fi_mod->find(array(
							'conditions' => 'fabric_shop_id = ' . $fabric_shop_id.' and item like \'%' . $factory_item."%'",
					));
				}
				if ($fabric_shop_id && $factory_item==-1){
					$fi = $fi_mod->find(array(
							'conditions' => 'fabric_shop_id = ' . $fabric_shop_id,
					));
				}
				if ($factory_item!=-1 && !$fabric_shop_id){
					$fi = $fi_mod->find(array(
							'conditions' => 'item like \'%' . $factory_item."%'",
					));
				}
				if (!empty($fi)) {
					$this->assign('fabric_shop_id', $fabric_shop_id);
					$sample_ids = array();
					foreach ($fi as $val) {
						if (!in_array($val['sample_id'], $sample_ids) && $val['sample_id']!='') {
							$sample_ids[] = $val['sample_id'];
						}
					}
					//print_r ($sample_ids);
					$ids = implode(',', $sample_ids);
					if($ids != "")
					{
						$conditions .= ($conditions ? ' AND ' : '') . 'sample.sample_id in (' . $ids . ')';
					}
					else
					{
						$conditions .= ($conditions ? ' AND ' : '') . 'sample.sample_id = 0';
					}
				} 
				//echo $conditions;
				$is_order = !isset($_GET['is_order'])? -1 : intval($_GET['is_order']);
				if ($is_order != -1) {
					if($is_order==1)
					{
						$conditions .= ($conditions ? ' AND ' : '') .  "order_sn != ''";
					}
					else
					{
						$conditions .= ($conditions ? ' AND ' : '') .  "order_sn = ''";
					}
				}
				else
				{
					$_GET['is_order']=-1;
				}

				$this->assign('is_order', $is_order);

				$order = empty($_GET['order']) ? 'arrival_date DESC' : str_replace('-', ' ', trim($_GET['order']));
				if ($order != 'arrival_date DESC' || $order != 'arrival_date ASC') {
					$order .= ', arrival_date DESC';
				}
				$sample_list = $this->_sample_mod->find(array(
						'fields' => '*, sample.description',
						'conditions' => $conditions.' and sample.status!='.SAMPLE_STATUS_DELETE,
						'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_fabric,belongs_feature,belongs_special,belongs_factory',
						'order' => $order,
						'count' => true,
						'limit' => $page['limit']
				));


				foreach ($sample_list as $key => $val) {
					$buy_info=$merch_mod->get(array('conditions'=>'activation=1 and role='.USER_CLIENT.' and user_id='.$val['buyer_id']));
					if (!empty($buy_info)){
						$sample_list[$key]['member_name']=$buy_info['user_name'];
					}
					$to_factory_date=!empty($val['to_factory_date'])?intval($val['to_factory_date']):0;
					if ($val['status'] == SAMPLE_STATUS_SEND_SAMPLE || $val['status'] ==SAMPLE_STATUS_STOCK) {
						if (!$val['send_sample_date']) {
							$end_time = time();
						} else {
							$end_time = $val['send_sample_date'];
						}
						if($user['role']==USER_FACTORY)
						{
							$sample_list[$key]['sample_made_days'] = intval(($end_time - $to_factory_date) / 60 / 60 / 24) + 1;
						}
						else
						{
							$sample_list[$key]['sample_made_days'] = intval(($end_time - $val['arrival_date']) / 60 / 60 / 24) + 1;
						}
					}
					else
					{
						if($user['role']==USER_FACTORY)
						{
							$sample_list[$key]['sample_made_days'] = !empty($to_factory_date)?intval((time() - $to_factory_date) / 60 / 60 / 24) + 1:0;
						}
						else
						{
							$sample_list[$key]['sample_made_days'] =!empty($val['arrival_date'])? intval((time() - $val['arrival_date']) / 60 / 60 / 24) + 1:0;
						}
					}
				}
			}

			if (!isset($status)) {
				$this->assign('status', -1);
			}
			if (!isset($is_order)) {
				$this->assign('is_order', -1);
			}

			if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
			{
				$mod_company_manager=&m("company_manager");
				$brand_where=" AND brand.company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')})";
			}
			$this->assign('brand_list', $brand_mod->find(array('conditions'=>'if_show = 1'.$brand_where,'order'=>'brand_name','join'=>"belong_company",)));

			$buyer_mod =& m('member');
			$buyer_list = $buyer_mod->find(array(
					'conditions' => 'if_show = 1 and activation=1 and role='.USER_CLIENT,
					'order' => 'user_name ASC'
			));
			$this->assign('buyer_list',$buyer_list);
			
			$this->assign('merch_list', $merch_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_MERCHANDISER,'order'=>'user_name')));

			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1','order'=>'style_name')));

			$fabric_mod =& m('fabric');
			$this->assign('fabric_list', $fabric_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_name ASC')));
				
			$feature_mod =& m('feature');
			$this->assign('feature_list', $feature_mod->find(array('conditions'=>'if_show = 1', 'order'=>'feature_name ASC')));

			$special_mod =& m('special');
			$this->assign('special_list', $special_mod->find(array('conditions'=>'if_show = 1','order'=>'special_name')));

			$factory_mod =& m('factory_info');
			if($user_info[role]==USER_QC){
				if(empty($arr_info)){
					$where = "1=1 and factory_id in ('') and activation=1";
				}else{
					$where = "1=1 and factory_id in (".$arr_info.") and activation=1";
				}
			}else{
				$where = "1=1 and activation=1";
			}
			$this->assign('factory_list', $factory_mod->find(array('join'=>'belongs_factory_member','conditions'=>$where,'order'=>'factory_name')));

			$fs_mod =& m('fabric_shop');
			$this->assign('fabric_shop_list', $fs_mod->find(array('conditions'=>'if_show = 1','order'=>'fabric_shop_name')));

			$cc_mod = & m('color_card');
			$fabric_info_mod = &m('fabric_info');
			$color_card_info_list=array();


			if(empty($fabric_shop_id))
			{
				$color_card_info_list=$fabric_info_mod->getall("select item from {$fabric_info_mod->table} group by item order by item");
			}
			else
			{
				$color_card_info_list=$fabric_info_mod->getall("select item from {$fabric_info_mod->table} where fabric_shop_id={$fabric_shop_id} group by item order by item") ;

			}
			//print_r($fabric_info_mod);
			$color_card_info_list_val=array();
			foreach($color_card_info_list as $key=>$value){
				$color_card_info_list_val[$key]['item'] = $value['item'];
			}
			$this->assign('color_card_info_list',$color_card_info_list_val);
			if ($this->visitor->get('role') == USER_MANAGER) {
				$this->assign('manager', true);
			}

			if (!isset($_GET['is_order']))
			{
				$_GET['is_order']=-1;
			}
		}
		$page['item_count'] = $this->_sample_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sample_list', $sample_list);
		$this->display('sample.search.html');
		$this->_curlocal(Lang::get('sample_search'));
	}




	function _assign_user_list($role = 0, $assign = 'user_list')
	{
		$user_mod =& m('member');
		$conditions = 'if_show = 1';
		if ($role) {
			$conditions .= ' and role = ' . $role;
		}
		$user_list = $user_mod->find(array(
				'conditions' => $conditions,
				'order' => 'user_name asc',
		));
		$this->assign($assign, $user_list);
	}

	function _assign_buyer_list()
	{
		$buyer_mod =& m('member');
		$buyer_list = $buyer_mod->find(array(
				'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
				'order' => 'user_name asc',
		));
		$this->assign('buyer_list', $buyer_list);
	}

	function more_reset()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_CIVILIAN, USER_MANAGER));
		$ids = empty($_GET['ids']) ? '' : trim($_GET['ids']);
		if (!$ids) {
			$this->show_warning('select_reseted_sample_please');
			return;
		}
		$ids = explode(',', $ids);
		$num = 0;
		foreach ($ids as $id) {
			$sample = $this->_sample_mod->get_info($id);
			if ($sample && $sample['status'] != SAMPLE_STATUS_CREATE) {
				$data = array(
						'is_stock ' => 0,
						'stock_date ' => 0,
						'confirm_date ' => 0,
						'status' => SAMPLE_STATUS_CREATE
				);
				$this->_sample_mod->edit($id, $data);
				if (!$this->_sample_mod->get_error()) {
					$num += 1;
				}
			}
		}
		$this->show_message('批量重置 ' . count($ids) . ' 个样板，成功 ' . $num . ' 个。');
	}

	function edit_desc()
	{
		if (!IS_POST) {
			echo '数据提交错误！';
		} else {
			$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
			if (!$id) {
				echo Lang::get('sample_not_exist');
				return;
			}
			$description = trim($_POST['description']);
			$this->_sample_mod->edit($id, array('description'=>$description));
			if ($this->_sample_mod->get_error()) {
				echo $this->_sample_mod->get_error();
				return;
			}
			echo 'success';
		}
	}
	
	
	//删除面料信息
	function drop_fabric(){
		$f_id=isset($_GET['fab_id'])?intval($_GET['fab_id']):0;
		$result=array();
		if (!$f_id){
			$result['msg']="网络连接错误，请稍后再试";
			$result['flag']=false;
			exit(json_encode($result));
		}
		$fabric_mod=&m('fabric_info');
		$fabric_mod->drop($f_id);
		if ($fabric_mod->has_error()){
			$this->show_warning($fabric_mod->get_error());
			exit;
		}else{
			$result['msg']="删除成功";
			$result['flag']=true;
			exit(json_encode($result));
		}
	}

	function edit()
	{
		/* 检查权限 */
		$this->check_powers(array(USER_MERCHANDISER, USER_ASSISTANT));

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = ' . $id,
				'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}


		if ($sample['status'] == SAMPLE_STATUS_STOCK) {
			header('location:index.php?app=sample&act=show&id=' . $id);
		}
		
		$this->_assign_next_sample($id);
		
		//限制那些状态只能哪些角色编辑
		if (!$this->_check_sample_is_edit($sample)) {
			$this->show_warning('sample_not_exist');
			return;
		}

		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>'sample_id = ' . $id,
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));
		if (!empty($fabric_info_list)){
			$file_mod =& m('upload');
			foreach ($fabric_info_list as $key=>$rows){
				$f_id=$rows['fabric_info_id'];
				$f_path=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_FABRIC_INFO . ' AND key_id = ' . $f_id,
						'order' => 'file_id ASC',
				));
				$fabric_info_list[$key]['fabric_file']=$f_path;
			}
		}
		$this->assign('fabric_info_list', $fabric_info_list);
		$user_info=$this->visitor->get();
		if (!IS_POST) {
			//赋值用户信息
			$this->assign("user",$user_info);

			$this->_assign_user_list(USER_MERCHANDISER, 'merchandiser_list');

			$this->_assign_buyer_list();
			$this->_assign_brand_list();

			//赋值进度记录
			$mod_pro_records=&m("pro_records");
			$pro_records_list=$mod_pro_records->find(array(
					'conditions'=>"key_id={$id} AND type=".PRO_RECORDS_SAMPLES,
					'order'	=>"rec_time desc"
							));
			$this->assign("pro_records_list",$pro_records_list);

			//赋值色卡信息
			$colorcard_mode=& m('color_card');

			$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");

			$this->assign("fabric_shop_list",$fabric_shop_list);

			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));

			$fabricshop_mod =& m('fabric_shop');
			$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));

			$factory_mod =& m('factory_info');
			$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));

			$user_mod =& m('member');
			$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));

			$file_mod =& m('upload');
			//细节图
			$sample['consumer_sample_thumb'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND (key_id = ' . $id.' or (session_id="'.session_id().'" and key_id='.DEFAULT_CUSTOMER_KEY_ID.'))',
					'order' => 'file_id ASC',
			));
			//客户上传的附件start
			$sample['customer_reviews_upload'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_CONSUMER_REVIEWS . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			$sample['consumer_remark_upload'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_CONSUMER_REMARK . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			//end
			$sample['colorcard_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_COLORCARD . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			$sample['factory_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			$sample['customer_reviews_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_SAMPLE_CUSTOMER_REVIEWS . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			if ($user_info['role']!=USER_SAMPLE){
				$sample['makepanel_files'] = $file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_MAKEPANEL . ' AND key_id = ' . $id,
						'order' => 'file_id ASC',
				));
			}

			$sample['file_size'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_SAMPLE_FILESIZE . ' AND key_id = ' . $id
			));
			
// 			$sample['sample_pattern'] = $file_mod->find(array(
// 					'conditions' => 'file_type = ' . BELONGS_SAMPLE_PATTERN . ' AND key_id = ' . $id
// 			));
			
// 			$this->assign('belongs_sample_pattern',BELONGS_SAMPLE_PATTERN);
			$this->assign('belongs_colorcard', BELONGS_COLORCARD);
			$this->assign('belongs_factory', BELONGS_FACTORY);
			$this->assign('belongs_sample_customer_reviews', BELONGS_SAMPLE_CUSTOMER_REVIEWS);
			$this->assign('belongs_sample_makepanel', BELONGS_SAMPLE_MAKEPANEL);
			$this->assign('belongs_sample_filesize', BELONGS_SAMPLE_FILESIZE);
			$this->assign('belongs_consumer_reviews',BELONGS_CONSUMER_REVIEWS);
			$this->assign('belongs_consumer_remark',BELONGS_CONSUMER_REMARK);
			$this->assign('belongs_sample_thumb',BELONGS_SAMPLE_THUMB_IMG);
			$this->assign('belongs_fabric_info',BELONGS_FABRIC_INFO);
			$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->assign('sample', $sample);
			$this->display('sample.form1.html');
			$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('more_info'));
		} else {
			$data = array();
			$arrival_date = strtotime(trim($_POST['arrival_date']));
			$brand_id = intval($_POST['brand_id']);
			$buyer_id = intval($_POST['buyer_id']);
			$merchandiser_id = intval($_POST['merchandiser_id']);
			$ref_sample_style = intval($_POST['ref_sample_style']);
			$sample_sn = trim($_POST['sample_sn']);
			$front_pic = trim($_POST['front_pic']);
			$front_pic_small = trim($_POST['front_pic_small']);
			$other_pic = trim($_POST['other_pic']);
			$customer_reviews_text = trim($_POST['customer_reviews_text']);

			$style_id = intval($_POST['style_id']);
			$factory_id = intval($_POST['factory_id']);
			$to_factory_date = strtotime($_POST['to_factory_date']);
			$send_sample_date = strtotime($_POST['send_sample_date']);
			$predict_assign_date = strtotime($_POST['predict_assign_date']);
			$factory_price = trim($_POST['factory_price']);
			$description = trim($_POST['description']);
			$order_sn = trim($_POST['order_sn']);
			$make_panel_reviews = trim($_POST['make_panel_reviews']);
			$submit_val= trim($_POST['submit']);
			$approve_no_desc=isset($_POST['approve_no_desc'])?trim($_POST['approve_no_desc']):"";
			$return_factory_desc=isset($_POST['return_factory_desc'])?trim($_POST['return_factory_desc']):"";


			$data['factory_price'] = $factory_price;
			$data['predict_assign_date'] = $predict_assign_date;
			//当用户类型不为工厂时才可编辑以下字段
			if($user_info['role'] != USER_FACTORY)
			{
// 				if (!$arrival_date) {
// 					$this->show_warning('arrival_date_not_empty');
// 					return;
// 				}
// 				if (!$brand_id) {
// 					$this->show_warning('brand_select_pls');
// 					return;
// 				}
				if (!$buyer_id) {
					$this->show_warning('buyer_select_pls');
					return;
				}
				if (!$merchandiser_id) {
					$this->show_warning('merch_select_pls');
					return;
				}
				if (empty($sample_sn)) {
					$this->show_warning('sample_sn_not_empty');
					return;
				}
// 				if (!$ref_sample_style) {
// 					$this->show_warning('ref_sample_style_select_pls');
// 					return;
// 				}
				if ($front_pic) {
					$data['front_pic'] = $front_pic;
					$data['front_pic_small'] = $front_pic_small;
				}
				if ($other_pic) {
					$data['other_pic'] = $other_pic;
				}
				$data['style_id'] = $style_id;
				$data['factory_id'] = $factory_id;
				$data['description'] = $description;
				$data['to_factory_date'] = $to_factory_date;
				$data['customer_reviews_text'] = $customer_reviews_text;
				$data['make_panel_reviews'] = $make_panel_reviews;
				$data['order_sn'] = $order_sn;
				$data['arrival_date'] = $arrival_date;
				$data['brand_id'] = $brand_id;
				$data['buyer_id'] = $buyer_id;
				$data['merchandiser_id'] = $merchandiser_id;
				$data['sample_sn'] = $sample_sn;
				$data['ref_sample_style'] = $ref_sample_style;
				$consumer_price=trim($_POST['consumer_price']);
				if ($consumer_price){
					$data['consumer_price']=$consumer_price;
				}
					
				if ($to_factory_date) {
					$data['send_sample_date'] = $send_sample_date;
				}
					
				if ($data['send_sample_date']) {
					$data['sample_made_days'] = intval(($data['send_sample_date'] - $data['to_factory_date']) / 60 / 60 / 24) + 1;
				}
			}


			$mod_pro_records=&m("pro_records");
			$log_time=gmtime();

			switch($sample['status'])
			{
				case SAMPLE_STATUS_CREATE:		//新建
					if($submit_val=="送主管审批")
					{
						$data['status'] = SAMPLE_STATUS_APPROVE;
						$log_time=gmtime();
						$pro_data=array(
								"type"=>PRO_RECORDS_SAMPLES,
								"key_id"=>$id,
								"rec_name"=>$this->visitor->get("user_name"),
								"rec_buff"=>"送主管审批",
								"rec_time"=>$log_time
						);
						if(!$mod_pro_records->add($pro_data))
						{
							$this->show_warning($mod_pro_records->get_error());
							return;
						}

					}
					break;
				case SAMPLE_STATUS_APPROVE:		//主管审批

					$pro_data=array();
					if ($submit_val=="审批通过") {
						$data['status'] = SAMPLE_STATUS_SEND_FACTORY;
						$pro_data=array(
								"type"=>PRO_RECORDS_SAMPLES,
								"key_id"=>$id,
								"rec_name"=>$this->visitor->get("user_name"),
								"rec_buff"=>"主管审批通过",
								"rec_time"=>$log_time
						);
					}
					elseif($submit_val=="审批不通过")
					{
						if (empty($approve_no_desc)) {
							$this->show_warning('请输入审批不通过理由');
							return;
						}
						$data['status'] = SAMPLE_STATUS_CREATE;
						$pro_data=array(
								"type"=>PRO_RECORDS_SAMPLES,
								"key_id"=>$id,
								"rec_name"=>$this->visitor->get("user_name"),
								"rec_buff"=>"审批不通过,理由:{$approve_no_desc}",
								"rec_time"=>$log_time
						);
					}
					if(!empty($pro_data))
					{
						if(!$mod_pro_records->add($pro_data))
						{
						 $this->show_warning($mod_pro_records->get_error());
						 return;
						}
					}
					break;
				case SAMPLE_STATUS_SEND_FACTORY:		//已送工厂
					if ($submit_val=="提交样板") {
						$data['status'] = SAMPLE_STATUS_FACTORY_SUBMIT;
					}
					break;
				case SAMPLE_STATUS_FACTORY_SUBMIT:		//提交样板
					if ($submit_val=="寄板") {
						$data['status'] = SAMPLE_STATUS_SEND_SAMPLE;
						$data['confirm_date'] = time();

						if (empty($send_sample_date)) {
							$this->show_warning('请输入寄板日期');
							return;
						}
					}
					elseif($submit_val=="退工厂")
					{
						if (empty($return_factory_desc)) {
							$this->show_warning('请输入退工厂理由');
							return;
						}
						$data['status'] = SAMPLE_STATUS_SEND_FACTORY;

						$pro_data=array(
								"type"=>PRO_RECORDS_SAMPLES,
								"key_id"=>$id,
								"rec_name"=>$this->visitor->get("user_name"),
								"rec_buff"=>"退工厂,理由:{$return_factory_desc}",
								"rec_time"=>$log_time
						);
						if(!$mod_pro_records->add($pro_data))
						{
						 $this->show_warning($mod_pro_records->get_error());
						 return;
						}
					}
					break;

			}
			$this->_sample_mod->edit($id, $data);
			if ($this->_sample_mod->get_error())
			{
				$this->_error($this->_sample_mod->get_error());
				return;
			}
			
			//当送主管审批和审批通过发邮件
			$new_sample = $this->_sample_mod->get(array(
					'fields' => '*,sample.description',
					'conditions' => 'sample_id = ' . $id,
					'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
			));
			$user_mod=&m("member");
			if($sample['status']==SAMPLE_STATUS_CREATE&&$new_sample['status']==SAMPLE_STATUS_APPROVE)  //送主管审批时发送邮件
			{
				$manger_list=$user_mod->find(array(
						"conditions"=>"role ".db_create_in(array(USER_MANAGER,USER_MERCHANDISER_ADMIN))
				));
				$mail = get_mail('tomanger', array('sample' => $new_sample));
				foreach($manger_list as $item)
				{
					if(!empty($item['email']))
					{
						$this->_mailto($item['email'], addslashes($mail['subject']), addslashes($mail['message']));
					}
				}
			}
			elseif($sample['status']==SAMPLE_STATUS_APPROVE&&$new_sample['status']==SAMPLE_STATUS_SEND_FACTORY)  //审批通过发送邮件
			{
				$factory_id=!empty($new_sample['factory_id'])?$new_sample['factory_id']:"0";
				$merchandiser_id=$new_sample['merchandiser_id'];
				$factory_info=$user_mod->get(array(
					'join'=>'has_factory',
					'conditions'=>"factory_info.factory_id={$factory_id} and activation=1"		
				));
				$merchandiser_info=$user_mod->get("user_id={$merchandiser_id}");
				$mail = get_mail('tofactory', array('sample' => $new_sample));

				if(!empty($factory_info) || !empty($factory_info['email']))
				{
					$this->_mailto($factory_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
				}
				if(!empty($merchandiser_info) || !empty($merchandiser_info['email']))
				{
					$this->_mailto($merchandiser_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
				}
			}
			elseif($sample['status']==SAMPLE_STATUS_APPROVE&&$new_sample['status']==SAMPLE_STATUS_CREATE)     //审批不通过发送邮件
			{
				$factory_id=!empty($new_sample['factory_id'])?$new_sample['factory_id']:"0";
				$merchandiser_id=$new_sample['merchandiser_id'];

				$factory_info=$user_mod->get(array(
					'join'=>'has_factory',
					'conditions'=>"factory_info.factory_id={$factory_id} and activation=1"		
				));
				$merchandiser_info=$user_mod->get("user_id={$merchandiser_id}");

				$mail = get_mail('tofactory_approve_no', array('sample' => $new_sample,"approve_no_desc"=>$approve_no_desc));

				if(!empty($merchandiser_info) || !empty($merchandiser_info['email']))
				{
					$this->_mailto($merchandiser_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
				}
			}
			elseif($sample['status']==SAMPLE_STATUS_FACTORY_SUBMIT&&$new_sample['status']==SAMPLE_STATUS_SEND_FACTORY)    //退工厂发送邮件
			{
				$factory_id=!empty($new_sample['factory_id'])?$new_sample['factory_id']:"0";

				$factory_info=$user_mod->get(array(
					'join'=>'has_factory',
					'conditions'=>"factory_info.factory_id={$factory_id} and activation=1"		
				));

				$mail = get_mail('return_factory_mail', array('sample' => $new_sample,"return_factory_desc"=>$return_factory_desc));

				if(!empty($factory_info) || !empty($factory_info['email']))
				{
					$this->_mailto($factory_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
				}
			}
			elseif($sample['status']==SAMPLE_STATUS_SEND_FACTORY &&$new_sample['status']==SAMPLE_STATUS_FACTORY_SUBMIT)    //提交样板发送邮件
			{
				$merchandiser_id=$new_sample['merchandiser_id'];

				$merchandiser_info=$user_mod->get("user_id={$merchandiser_id}");

				$mail = get_mail('submit_sample', array('sample' => $new_sample));

				if(!empty($merchandiser_info) || !empty($merchandiser_info['email']))
				{
					$this->_mailto($merchandiser_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
				}
			}

			if(!$this->_check_sample_is_edit($new_sample))
			{
				header('location:index.php?app=sample&act=show&id=' . $id);
			}
			else
			{
				header('location:index.php?app=sample&act=edit&id=' . $id);
			}

		}
	}

	//采购页面
	function purchase(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$view=isset($_GET['print'])?trim($_GET['print']):'';
		$sour_mod=&m('sourcing_info');
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		if ($view=='view'){
			$this->assign('is_view',true);
		}
		$sample = $sour_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sourcing.sourcing_id = ' . $id,
				'join' => 'belongs_sample',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
		
		
		if ($sample['status'] == SAMPLE_STATUS_STOCK) {
			header('location:index.php?app=sample&act=show&id=' . $sample['sample_id']);
		}
		
		$this->_assign_next_sample($sample['sample_id']);
		
		//限制那些状态只能哪些角色编辑
		if (!$this->_check_sample_is_edit($sample)) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>'sample_id = ' . $sample['sample_id'].' and bulk_id='.$id,
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,bulk_id,bulk_status",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));
		if (!empty($fabric_info_list)){
			foreach ($fabric_info_list as $fabrics){
				$fabric_id[]=$fabrics['fabric_info_id'];
			}
		}
		$this->assign('fabric_info_list', $fabric_info_list);
		$user_info=$this->visitor->get();
		if (!IS_POST) {
			//赋值用户信息
			$this->assign("user",$user_info);
		
			$this->_assign_user_list(USER_MERCHANDISER, 'merchandiser_list');
		
			$this->_assign_buyer_list();
			$this->_assign_brand_list();
		
			//赋值色卡信息
			$colorcard_mode=& m('color_card');
		
			$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
		
			$this->assign("fabric_shop_list",$fabric_shop_list);
		
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
		
			$fabricshop_mod =& m('fabric_shop');
			$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
		
			$factory_mod =& m('factory_info');
			$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
		
			$user_mod =& m('member');
			$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));
		
			$file_mod=&m('upload');
			$sample['sample_sour_upload'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_SOURCING_INVOICES . ' AND key_id ' . db_create_in($fabric_id),
					'order' => 'file_id ASC',
			));
			$this->assign('belongs_sour_invoice',BELONGS_SOURCING_INVOICES);
			$this->assign('sample', $sample);
			$this->display('purchase.first.html');
			$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('purchase_info'));
		}else{
			$submit_val= trim($_POST['submit']);
			
			$mod_pro_records=&m("pro_records");
			$log_time=gmtime();
			$data=array();
			switch($sample['status'])
			{
				case SAMPLE_STATUS_SEND_FACTORY:		//已送工厂
					if ($submit_val=="确认"){
						$data['sourcing_status']=SOURCING_STATUS_SAMPLE_CONFIRM;
					}elseif ($submit_val=="重置"){
						$pro_data=array(
								"type"=>PRO_RECORDS_SAMPLES,
								"key_id"=>$sample['sample_id'],
								"rec_name"=>$this->visitor->get("user_name"),
								"rec_buff"=>"重置采购单",
								"rec_time"=>$log_time
						);
						if(!$mod_pro_records->add($pro_data))
						{
							$this->show_warning($mod_pro_records->get_error());
						}
						if (!empty($sample)){
							$data['sourcing_status']=SOURCING_STATUS_CREATE;
							$data['if_print']=0;
						}
					}elseif ($submit_val=="提交"){
						$data['sourcing_status']=SOURCING_STATUS_SOUR_SUBMIT;
					}elseif ($submit_val=="财务审核"){
						$data['sourcing_status']=SOURCING_STATUS_FINANCE_EXAMINE;
					}
					break;
			}
			$sour_mod->edit($id,$data);
			if ($sour_mod->has_error()){
				$this->show_warning($sour_mod->get_error());
				return;
			}
			//	print_r($_POST);exit();
			$new_fabric_ids=array();
			
			foreach($_POST['price'] as $key=>$item)
			{
				$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
				$fabric_data=array(
						"sample_id"=>$sample['sample_id'],
						"bulk_id"=>$id,
						"bulk_status"=>$data['sourcing_status'],
						"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
						"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
						"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
						"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
						"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
						"colors"=>isset($_POST['colors'][$key])?trim($_POST['colors'][$key]):"",
						"fabric_num"=>isset($_POST['fabric_num'][$key])?trim($_POST['fabric_num'][$key]):"",
						"address"=>isset($_POST['address'][$key])?trim($_POST['address'][$key]):"",
						"phone_mob"=>isset($_POST['phone_mob'][$key])?trim($_POST['phone_mob'][$key]):"",
				);
				if(!empty($fabric_data['price']))
				{
					if(empty($fabric_info_id))
					{
						$fabric_info_mod->add($fabric_data);
					}
					else
					{
						$new_fabric_ids[]=$fabric_info_id;
			
						$fabric_info_mod->edit("fabric_info_id={$fabric_info_id}",$fabric_data);
					}
				}
			}
			
			//修改采购总金额
			if (!empty($_POST['price'])){
				$amount=array_sum($_POST['price']);
				if($amount!=$sample['amount']){
					if ($amount){
						$sour_mod->edit($id,array('amount'=>$amount));
					}
				}
			}
			
			foreach($fabric_info_list as $item)
			{
				if(!in_array($item['fabric_info_id'],$new_fabric_ids))
				{
					$fabric_info_mod->drop("fabric_info_id={$item['fabric_info_id']}");
				}
			}
			
			if(!$this->_check_sample_is_edit($sample))
			{
				header('location:index.php?app=sample&act=show&id=' . $sample['sample_id']);
			
				//$this->show_message('update_sample_base_info_success', 'sample_info', "index.php?app=sample&act=show&id=$id");
			}
			else
			{
				header('location:index.php?app=sample&act=purchase&id='.$id);
			}
		}
	}
	
	
	//采购面料详情页面
	function purchase_info(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$sour_mod=&m('sourcing_info');
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample.sample_id = ' . $id,
				'join' => 'has_sourcing_info',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
	
	
		if ($sample['status'] == SAMPLE_STATUS_STOCK) {
			header('location:index.php?app=sample&act=show&id=' . $id);
		}
	
		$this->_assign_next_sample($id);
	
		//限制那些状态只能哪些角色编辑
		if (!$this->_check_sample_is_edit($sample)) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>'sample_id = ' . $id.' and bulk_id is null',
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,bulk_id,bulk_status",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));
		if (!empty($fabric_info_list)){
			foreach ($fabric_info_list as $fabrics){
				$fabric_id[]=$fabrics['fabric_info_id'];
			}
		}
		$this->assign('fabric_info_list', $fabric_info_list);
		$user_info=$this->visitor->get();
		if (!IS_POST) {
			//赋值用户信息
			$this->assign("user",$user_info);
	
			$this->_assign_user_list(USER_MERCHANDISER, 'merchandiser_list');
	
			$this->_assign_buyer_list();
			$this->_assign_brand_list();
	
			//赋值色卡信息
			$colorcard_mode=& m('color_card');
	
			$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
	
			$this->assign("fabric_shop_list",$fabric_shop_list);
	
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
	
			$fabricshop_mod =& m('fabric_shop');
			$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
	
			$factory_mod =& m('factory_info');
			$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
		
			$this->assign('sample', $sample);
			$this->display('purchase.fabric.html');
			$this->_curlocal(Lang::get('purchase_list'), 'index.php?app=sample&act=index', Lang::get('purchase_info'));
		}else{
			$submit_val= trim($_POST['submit']);
			$data=array();
			$new_bulk_ids=array();
			$bulk_arr_id=array();
			if (!empty($fabric_info_list)){
				foreach ($fabric_info_list as $rows){
					$bulk_arr_id[]=$rows['fabric_info_id'];
				}
			}
			$fabric_bulk_ids=empty($_POST['bulk_id'])?0:$_POST['bulk_id'];
			$f_bulk=array_filter($fabric_bulk_ids);
			$bulk_diff=count($fabric_bulk_ids)-count($f_bulk);
			if ((empty($fabric_bulk_ids) && count($fabric_bulk_ids)>1) || $bulk_diff>1){
				switch($sample['status'])
				{
					case SAMPLE_STATUS_SEND_FACTORY:		//已送工厂
					if ($submit_val=="保存"){
						if ($user_info['role']==USER_MERCHANDISER){
							$data['sourcing_status']=SOURCING_STATUS_CREATE;
							$data['sample_id']=$id;
							$data['add_time'] =gmtime();
							$bulk_id=$sour_mod->add($data);
							if (!$bulk_id){
								$this->show_warning($sour_mod->get_error());
								exit;
							}
							if (strlen ( $bulk_id ) <= 10) {
								$k = 10 - strlen ( $bulk_id );
								for($i = 0; $i < $k; $i ++) {
									$len .= 0;
								}
								$bulk_sn = 'CG' . $len . $bulk_id;
							}
							$sour_mod->edit($bulk_id,array('sourcing_sn'=>$bulk_sn));
							if ($sour_mod->has_error()){
								$this->show_warning($sour_mod->get_error());
								exit;
							}
						}
					}
					break;
				}
			}
			foreach($_POST['price'] as $key=>$item)
			{
				$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
				$bulk_data=array(
						"sample_id"=>$id,
						"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
						"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
						"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
						"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
						"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
						"colors"=>isset($_POST['colors'][$key])?trim($_POST['colors'][$key]):"",
						"fabric_num"=>isset($_POST['fabric_num'][$key])?trim($_POST['fabric_num'][$key]):"",
						"address"=>isset($_POST['address'][$key])?trim($_POST['address'][$key]):"",
						"phone_mob"=>isset($_POST['phone_mob'][$key])?trim($_POST['phone_mob'][$key]):"",
				);
				if(!empty($bulk_data['price']))
				{
					if(empty($fabric_info_id) || !in_array($fabric_info_id, $bulk_arr_id))
					{
						if ($submit_val=="保存"){
							if ($bulk_id){
								$bulk_data['bulk_status']=SOURCING_STATUS_CREATE;
								$bulk_data['bulk_id']=$bulk_id;
							}
						}
						$fabric_info_mod->add($bulk_data);
					}
					else
					{
						if ($submit_val=="保存"){
								$bulk_info=$fabric_info_mod->get($fabric_info_id);
								if(empty($bulk_info['bulk_id'])){
									if ($bulk_id){
										$bulk_data['bulk_status']=SOURCING_STATUS_CREATE;
										$bulk_data['bulk_id']=$bulk_id;
									}
								}
							}
							$new_bulk_ids[]=$fabric_info_id;
							
						$fabric_info_mod->edit("fabric_info_id={$fabric_info_id}",$bulk_data);
					}
				}
			}
			
			//修改采购总金额
			if ($bulk_id){
				$bulk_amount = $fabric_info_mod->find(array(
						'conditions'=>'sample_id = ' . $id.' and bulk_id='.$bulk_id,
						'fields'=>"price"
				));
				if (!empty($bulk_amount)){
					foreach ($bulk_amount as $res){
						$amount[]=$res['price'];
					}
					$amounts=array_sum($amount);
					if ($amounts && $bulk_id){
						$sour_mod->edit($bulk_id,array('amount'=>$amounts));
					}
				}
			}
			
			if(!empty($fabric_shop_list)){
				foreach($fabric_info_list as $item)
				{
					if(!in_array($item['fabric_info_id'],$new_bulk_ids))
					{
						$fabric_info_mod->drop("fabric_info_id={$item['fabric_info_id']}");
					}
				}
			}
			
			if(!$this->_check_sample_is_edit($sample))
			{
				header('location:index.php?app=sample&act=show&id=' . $id);
					
				//$this->show_message('update_sample_base_info_success', 'sample_info', "index.php?app=sample&act=show&id=$id");
			}
			else
			{
				if ($bulk_id){
					header('location:index.php?app=sample&act=purchase&id='.$bulk_id);
				}else{
					header('location:index.php?app=sample&act=purchase_info&id='.$id);
				}
			}
		}
	}
	
	
	//打印采购单
	function print_purchase(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$sid = empty($_GET['sid']) ? 0 : intval($_GET['sid']);
		$sour_mod=&m('sourcing_info');
		$user_info=$this->visitor->get();
		if (!$id && !$sid) {
			$this->show_warning('sample_not_exist');
			return;
		}
		if ($id){
			$sample = $this->_sample_mod->get(array(
					'fields' => '*,sample.description',
					'conditions' => 'sample.sample_id = ' . $id,
					'join' => 'has_sourcing_info',
			));
		}else{
			$sample = $sour_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sourcing.sourcing_id = ' . $sid,
				'join' => 'belongs_sample',
			));
		}
		
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
		if(!in_array($user_info['role'], array(USER_MERCHANDISER,USER_SOURCING,USER_FINANCE)))
		{
			$this->show_warning('用户无权限打印此采购单');
			return;
		}
		
			
		$fabric_info_mod =& m('fabric_info');
		if ($id){
			$conditions="sample_id = " . $id." and bulk_id=0";
		}
		if ($sid){
			$conditions="sample_id = " . $sample['sample_id']." and bulk_id=".$sid;
		}
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>$conditions,
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));
		
		$this->assign('fabric_info_list', $fabric_info_list);
		
		//赋值用户信息
		$this->assign("user",$user_info);
			
		//赋值色卡信息
		$colorcard_mode=& m('color_card');
			
		$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
		
		$this->assign("fabric_shop_list",$fabric_shop_list);
			
		$style_mod =& m('style');
		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			
		$fabricshop_mod =& m('fabric_shop');
		$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
			
		$factory_mod =& m('factory_info');
		$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
		
		$user_mod =& m('member');
		$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));
		if (!empty($sample)){
			if ($sample['buyer_id']){
				$buyer_name=$user_mod->get(array(
						'conditions'=>'activation=1 and if_show=1 and user_id='.$sample['buyer_id'].' and role='.USER_CLIENT,
						'fields'	=>'user_name'
				));
				$sample['member_name']=$buyer_name['user_name'];
			}
			if ($sample['merchandiser_id']){
				$merch_name=$user_mod->get(array(
						'conditions'=>'if_show=1 and user_id='.$sample['merchandiser_id'].' and role='.USER_MERCHANDISER,
						'fields'	=>'user_name'
				));
				$sample['user_name']=$merch_name['user_name'];
			}
			if ($sample['brand_id']){
				$brand_mod=& m('brand');
				$brand_name=$brand_mod->get(array(
						'conditions'=>'if_show=1 and brand_id='.$sample['brand_id'],
						'fields'	=>'brand_name'
				));
				$sample['brand_name']=$brand_name['brand_name'];
			}
			if (empty($sample['if_print'])){
				$sour_mod->edit($sample['sourcing_id'],array('if_print'=>1));
				if ($sour_mod->has_error()){
					$this->show_warning($sour_mod->get_error());
					return;
				}
			}
		}
		$this->assign('sample', $sample);
		$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('more_info'));
		$this->display('purchase.print.html');
	}
	
	
	//添加面料信息到数据库中
	function new_add_fabric(){
		$s_id=isset($_GET['sample_id'])?intval($_GET['sample_id']):0;
		$result=array();
		$fabric_mod=&m('fabric_info');
		if (!$s_id){
			$result['msg']="没有搜索到该样板";
			$result['flag']=false;
			exit(json_encode($result));
		} 
		$sample_info=$this->_sample_mod->get($s_id);
		if (empty($sample_info)){
			$result['msg']="没有搜索到该样板";
			$result['flag']=false;
			exit(json_encode($result));
		}
		if ($_POST['price']){
			foreach($_POST['price'] as $key=>$item)
			{
				$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
				$fabric_data=array(
						"sample_id"=>$s_id,
						"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
						"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
						"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
						"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
						"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
						"width"=>isset($_POST['width'][$key])?trim($_POST['width'][$key]):"",
						"fabric_characteristics"=>isset($_POST['fabric_characteristics'][$key])?trim($_POST['fabric_characteristics'][$key]):""
				);
				//print_r($_POST);exit();
				if(!empty($fabric_data['price']))
				{
					if(empty($fabric_info_id))
					{
						$fabric_id=$fabric_mod->add($fabric_data);
					}
				}
			}
		}
		if ($fabric_id){
			$fabric_info=$fabric_mod->get(array(
				'join'=>'belongs_fabric_shop',
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
				'conditions'=>$fabric_id		
			));
			if (empty($fabric_info['fabric_shop_name'])){
				$fabric_info['fabric_shop_name']="";
			}
			if (empty($fabric_info['item'])){
				$fabric_info['item']="";
			}
			$fabric_arr_id=isset($_POST['fabric_arr_id'])?$_POST['fabric_arr_id']:"";
			$file_mod=&m('upload');
			if(!empty($fabric_arr_id))
			{
				$file_ids=implode(",",$fabric_arr_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_FABRIC_INFO." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
					
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $fabric_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$fabric_id,
							"session_id"=>session_id()
					));
				}
			}
			$result['flag']=true;
			$result['fab_data']=$fabric_info;
			exit(json_encode($result));
		}
	}
	
	
	function temp()
	{
		//$this->check_powers(USER_ASSISTANT);
		//$this->assign('is_temp', true);
		//$this->search();
		if (file_exists(ROOT_PATH . '/temp_sample.php')) {
			$sample_ids = include(ROOT_PATH . '/temp_sample.php');
		}
		if ($sample_ids) {
			$conditions = 'sample_id not in (' . $sample_ids . ')';
		}
		$page = $this->_get_page(20);
		$list = $this->_sample_mod->find(array(
				'conditions' => $conditions,
				'order' => 'arrival_date, sample_id ASC',
				'join' => 'belongs_brand,belongs_buyer,belongs_merchandiser,belongs_style,belongs_fabric,belongs_feature,belongs_factory',
				'count' => true,
				'limit' => $page['limit']
		));
		$page['item_count'] = $this->_sample_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sample_list', $list);
		$this->display('sample.temp.html');
	}

	function temp_edit()
	{
		$id = intval($_GET['id']);
		if (!$id) {
			echo '<script>alert("找不到样板！");</script>';
			return;
		}
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = ' . $id,
				'join' => 'belongs_brand,belongs_buyer,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array('conditions'=>'sample_id = ' . $id, 'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);
		if (!IS_POST) {
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			$file_mod =& m('upload');
			$sample['colorcard_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_COLORCARD . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			$sample['factory_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			$fabricshop_mod =& m('fabric_shop');
			$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
			$this->assign('sample', $sample);
			$this->display('sample.temp_edit.html');
		} else {
			/*$old_fabric_info = $_POST['old_fabric_info'];
			 foreach ($old_fabric_info as $key => $val) {
			if (trim($val['description']) && intval($val['fabric_shop_id'])) {
			$fabric_info_mod->edit($key, array('sample_id'=>$id,'description'=>trim($val['description']),'fabric_shop_id'=>intval($val['fabric_shop_id'])));
			}
			}
			$new_fabric_info = $_POST['new_fabric_info'];
			foreach ($new_fabric_info as $key => $val) {
			if (trim($val['description']) && intval($val['fabric_shop_id'])) {
			$fabric_info_mod->add(array('sample_id'=>$id,'description'=>trim($val['description']),'fabric_shop_id'=>intval($val['fabric_shop_id'])));
			}
			}*/
			$old_fabric_info = $_POST['old_fabric_info'];
			foreach ($fabric_info_list as $key => $val) {
				if (!isset($old_fabric_info[$val['fabric_info_id']])) {
					$fabric_info_mod->drop($val['fabric_info_id']);
				}
			}
			foreach ($old_fabric_info as $key => $val) {
				if (trim($val['description']) && intval($val['fabric_shop_id'])) {
					$fabric_info_mod->edit($key, array('sample_id'=>$id,'description'=>trim($val['description']),'fabric_shop_id'=>intval($val['fabric_shop_id'])));
				}
			}

			if (file_exists(ROOT_PATH . '/temp_sample.php')) {
				$sample_ids = include(ROOT_PATH . '/temp_sample.php');
				$sample_ids .= ',' . $id;
			} else {
				$sample_ids = $id;
			}
			$str = "<?php\r\n";
			$str .= "\treturn '" . $sample_ids . "';\r\n";
			$str .= "?>";
			file_put_contents(ROOT_PATH . '/temp_sample.php', $str);
			$this->_sample_mod->edit($id, $_POST['sample']);

			$this->show_message('更新成功！<script>setTimeout("window.close();", 2000);</script>', 'sample_info', "temp.php?id=$id");
		}
	}

	function data()
	{
		$this->check_powers(USER_MANAGER);

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = ' . $id,
				'join' => 'belongs_brand,belongs_buyer,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array('conditions'=>'sample_id = ' . $id, 'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);

		if (!IS_POST) {
			$style_mod =& m('style');
			$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));

			$fabricshop_mod =& m('fabric_shop');
			$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));

			$factory_mod =& m('factory_info');
			$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
		
			$user_mod =& m('member');
			$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));

			$file_mod =& m('upload');
			$sample['colorcard_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_COLORCARD . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));
			$sample['factory_files'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $id,
					'order' => 'file_id ASC',
			));

			$this->assign('belongs_colorcard', BELONGS_COLORCARD);
			$this->assign('belongs_factory', BELONGS_FACTORY);

			$this->assign('sample', $sample);
			$this->display('sample.form1.html');
			$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('more_info'));
		} else {
			$data = array();
			$front_pic = trim($_POST['front_pic']);
			$other_pic = trim($_POST['other_pic']);
			$style_id = intval($_POST['style_id']);
			$factory_id = intval($_POST['factory_id']);
			$to_factory_date = strtotime($_POST['to_factory_date']);
			$send_sample_date = strtotime($_POST['send_sample_date']);
			$customer_reviews_text = trim($_POST['customer_reviews_text']);
			$make_panel_reviews = trim($_POST['make_panel_reviews']);
			$factory_price = trim($_POST['factory_price']);
			$description = trim($_POST['description']);
			if ($front_pic) {
				$data['front_pic'] = $front_pic;
			}
			if ($other_pic) {
				$data['other_pic'] = $other_pic;
			}
			$data['style_id'] = $style_id;
			$data['factory_id'] = $factory_id;
			$data['factory_price'] = $factory_price;
			$data['description'] = $description;
			$data['to_factory_date'] = $to_factory_date;
			$data['send_sample_date'] = $send_sample_date;
			$data['customer_reviews_text'] = $customer_reviews_text;
			$data['$make_panel_reviews'] = $make_panel_reviews;
			$this->_sample_mod->edit($id, $data);
			if ($this->_sample_mod->get_error())
			{
				$this->_error($this->_sample_mod->get_error());
				return;
			}
			$new_fabric_info = $_POST['new_fabric_info'];
			foreach ($new_fabric_info as $key => $val) {
				if (trim($val['description']) && intval($val['fabric_shop_id'])) {
					$fabric_info_mod->add(array('sample_id'=>$id,'description'=>trim($val['description']),'fabric_shop_id'=>intval($val['fabric_shop_id'])));
				}
			}
			$old_fabric_info = $_POST['old_fabric_info'];
			foreach ($fabric_info_list as $key => $val) {
				if (!isset($old_fabric_info[$val['fabric_info_id']])) {
					$fabric_info_mod->drop($val['fabric_info_id']);
				}
			}
			foreach ($old_fabric_info as $key => $val) {
				if (trim($val['description']) && intval($val['fabric_shop_id'])) {
					$fabric_info_mod->edit($key, array('sample_id'=>$id,'description'=>trim($val['description']),'fabric_shop_id'=>intval($val['fabric_shop_id'])));
				}
			}
			$this->show_message('update_sample_base_info_success', 'sample_info', "index.php?app=sample&act=data&id=$id");
		}
	}

	function addrecord()
	{
		$buff=isset($_GET['buff'])?trim($_GET['buff']):'';
		$id=isset($_GET['id'])?intval($_GET['id']):0;
		if(!empty($buff)&&!empty($id))
		{
			$mod_pro_records=&m("pro_records");
			$data=array(
					"type"=>PRO_RECORDS_SAMPLES,
					"key_id"=>$id,
					"rec_name"=>$this->visitor->get("user_name"),
					"rec_buff"=>$buff,
					"rec_time"=>gmtime()
			);
			if(!$mod_pro_records->add($data))
			{
				$this->json_error($mod_pro_records->get_error());

			}
			else
			{
				$rec_time=gmtimeshow($data['rec_time'],"m-d");
				$this->json_result("<p>{$rec_time}&nbsp;:&nbsp;{$buff}&nbsp;&nbsp;({$data['rec_name']})</p>");
			}
		}
	}

	//ajax获取品名/货号列表
	function ajax_getitem()
	{
		$fabric_shop_id=isset($_GET['fabric_shop_id'])?intval($_GET['fabric_shop_id']):0;

		$colorcard_mode=& m('color_card');
		$item_list=$colorcard_mode->find(array(
				'conditions'=>"fabric_shop_id={$fabric_shop_id}",
				'fields'=>"item,cc_id",
				'order'	=>"update_time desc"
						));

		echo json_encode($item_list);

	}

	//ajax获取色卡信息
	function ajax_getcolorcard()
	{
		$cc_id=isset($_GET['cc_id'])?intval($_GET['cc_id']):0;

		$colorcard_mode=& m('color_card');
		$cc_info=$colorcard_mode->get(array(
				'conditions'=>"cc_id={$cc_id}"
		));

		echo json_encode($cc_info);

	}

	//打印样板
	function printsample()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = ' . $id,
				'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}

		if($this->visitor->get("role")==USER_FACTORY&&$sample['factory_id']!=$this->visitor->get("factory_id"))
		{
			$this->show_warning('用户无权限打印此样板');
			return;
		}

			
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>'sample_id = ' . $id,
				'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));

		$this->assign('fabric_info_list', $fabric_info_list);
		$user_info=$this->visitor->get();

		//赋值用户信息
		$this->assign("user",$user_info);
			
		//赋值进度记录
		$mod_pro_records=&m("pro_records");
		$pro_records_list=$mod_pro_records->find(array(
				'conditions'=>"key_id={$id} AND type=".PRO_RECORDS_SAMPLES,
				'order'	=>"rec_time desc"
						));
		$this->assign("pro_records_list",$pro_records_list);
			
		//赋值色卡信息
		$colorcard_mode=& m('color_card');
			
		$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");

		$this->assign("fabric_shop_list",$fabric_shop_list);
			
		$style_mod =& m('style');
		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
			
		$fabricshop_mod =& m('fabric_shop');
		$this->assign('fabricshop_list', $fabricshop_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
			
		$factory_mod =& m('factory_info');
		$this->assign('factory_list', $factory_mod->find(array('conditions'=>'if_show = 1 and activation=1', 'order'=>'factory_name', 'join'=>'belongs_factory_member')));
		
		$user_mod =& m('member');
		if ($sample['buyer_id']){
			$buyers=$user_mod->get(array(
					'fields'=>'user_name',
					'conditions'=>'user_id='.$sample['buyer_id'].' and role='.USER_CLIENT
			));
			$sample['buyer_name']=$buyers['user_name'];
		}
		$this->assign('sour_list', $user_mod->find(array('conditions'=>'if_show = 1 AND role = ' . USER_SOURCING, 'order'=>'user_name ASC')));

		$file_mod =& m('upload');
		$sample['colorcard_files'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_COLORCARD . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
		$sample['factory_files'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_FACTORY . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
			
		$sample['file_size'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_FILESIZE . ' AND key_id = ' . $id
		));
		//细节图
		$sample['consumer_sample_thumb'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND key_id = ' . $id,
				'order' => 'file_id ASC',
		));
		$this->assign('belongs_sample_thumb',BELONGS_SAMPLE_THUMB_IMG);
		$this->assign('belongs_colorcard', BELONGS_COLORCARD);
		$this->assign('belongs_factory', BELONGS_FACTORY);
		$this->assign('belongs_sample_filesize', BELONGS_SAMPLE_FILESIZE);
		$this->assign('sample', $sample);
		$this->display('sample.print.html');
		$this->_curlocal(Lang::get('sample_info'), 'index.php?app=sample&act=status', Lang::get('more_info'));
	}

	//待审批样板
	function approve()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		/* 获取品牌 */
		$brand_mod =& m('brand');
		if($this->visitor->get("role")==USER_MANAGER)
		{
			$this->assign('user_manage', USER_MANAGER);
		}
		if($this->visitor->get("role")==USER_MERCHANDISER_ADMIN)
		{
			$this->assign('user_manage', USER_MANAGER);
			$mod_company_manager=&m("company_manager");
			$brand_where=" AND brand.company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')})";
		}
		$brand_list = $brand_mod->find(array(
				'conditions' => 'if_show = 1'.$brand_where,
				'join'	=>'belong_company',
				'order' => 'brand_name ASC'
		));
		$this->assign('brand_list', $brand_list);

		/* 获取买手 */
		$buyer_mod =& m('member');
			$buyer_list = $buyer_mod->find(array(
					'conditions' => 'if_show = 1 and activation=1 and role='.USER_CLIENT,
					'order' => 'user_name ASC'
			));
		$this->assign('buyer_list', $buyer_list);

		/* 获取采购 */
		$user_mod =& m('member');
		$user_list = $user_mod->find(array(
				'conditions' => 'role = ' . USER_SOURCING . ' AND if_show = 1',
				'order' => 'user_name ASC'
		));
		$this->assign('user_list', $user_list);

		/* 获取工厂 */
		$factory_mod =& m('factory_info');
		$factory_list = $factory_mod->find(array(
			'join'=>'belongs_factory_member',
			'order' => 'factory_name ASC',
			'conditions'=>'activation=1'
		));
		$this->assign('factory_list', $factory_list);

		/* 获取跟单 */
		$merch_mod =& m('member');
		$merch_list = $merch_mod->find(array(
				'conditions' => 'role = ' . USER_MERCHANDISER . ' AND if_show = 1' . ($this->visitor->get('role') == USER_ASSISTANT ? ' AND user_id in (' . $this->visitor->get('merchandiser') . ')' : ''),
				'order' => 'user_name',
		));
		$this->assign('merch_list', $merch_list);

		/* 获取查询条件 */
		$conditions = array();
		$sample_sn = empty($_GET['sample_sn']) ? null : trim($_GET['sample_sn']);
		if ($sample_sn) {
			$conditions[] = "sample_sn = '$sample_sn'";
		} else {
			$arrival_date_start = empty($_GET['arrival_date_start']) ? 0 : strtotime(trim($_GET['arrival_date_start']));
			if ($arrival_date_start) {
				$conditions[] = "arrival_date >= $arrival_date_start";
			}
			$arrival_date_end = empty($_GET['arrival_date_end']) ? 0 : strtotime(trim($_GET['arrival_date_end']));
			if ($arrival_date_end) {
				$conditions[] = "arrival_date <= $arrival_date_end";
			}
			$brand_id = empty($_GET['brand_id']) ? 0 : intval($_GET['brand_id']);
			if ($brand_id) {
				$conditions[] = "sample.brand_id = $brand_id";
			}
			$buyer_id = empty($_GET['buyer_id']) ? 0 : intval($_GET['buyer_id']);
			if ($buyer_id) {
				$conditions[] = "sample.buyer_id = $buyer_id";
			}
			$factory_id = empty($_GET['factory_id']) ? 0 : intval($_GET['factory_id']);
			if ($factory_id) {
				$conditions[] = "sample.factory_id = $factory_id";
			}
			$status = !isset($_GET['status']) ? -1 : intval($_GET['status']);
			if ($status != -1) {
				$conditions[] = "sample.status = $status";
			} else {
				$_GET['status'] = -1;
			}
			$merch_id=empty($_GET['merch_id']) ? 0 : intval($_GET['merch_id']);
			if ($merch_id) {
				$conditions[] = "sample.merchandiser_id = $merch_id";
			}
		}
		$user = $this->visitor->get();
		if($this->visitor->get("role")==USER_MANAGER)
		{
			$conditions[] = "sample.status =".SAMPLE_STATUS_APPROVE;

		}elseif ($this->visitor->get("role")==USER_MERCHANDISER_ADMIN){
			$conditions[] = "sample.status =".SAMPLE_STATUS_APPROVE;
			$mod_company_manager=&m("company_manager");
			$brand_mod =& m('brand');
			$conditions[]=" sample.brand_id in(select brand_id from {$brand_mod->table} where company_id in(select company_id from {$mod_company_manager->table} where uid={$this->visitor->get('user_id')}))";
		}
		else
		{
			$conditions[] = "sample.status =-100";
		}

		$conditions = ($conditions ? ' AND ' : '') . implode(' AND ', $conditions);

		/* 获取排序 */
		$order = empty($_GET['order']) ? 'arrival_date DESC' : str_replace('-', ' ', trim($_GET['order']));

		if ($order != 'arrival_date DESC' || $order != 'arrival_date ASC') {
			$order .= ', arrival_date DESC';
		}

		$page = $this->_get_page();
		$sample_list = $this->_sample_mod->find(array(
				'fields' => '*, sample.status, sample.description',
				'conditions' => 'ref_sample_style != 3 AND sample.status != ' . SAMPLE_STATUS_STOCK . ' AND sample.status != ' . SAMPLE_STATUS_DELETE . $conditions,
				'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_factory',
				'order' => $order,
				'limit' => $page['limit'],
				'count' => true
		));

		$mod_pro_records=&m("pro_records");

		foreach ($sample_list as $key => $val) {
			$buy_info=$user_mod->get($val['buyer_id']);
			if (!empty($buy_info)){
				$sample_list[$key]['member_name']=$buy_info['user_name'];
			}
			if ($val['status'] >= SAMPLE_STATUS_SEND_FACTORY && $val['status'] < SAMPLE_STATUS_STOCK) {
				if (!$val['send_sample_date']) {
					$end_time = time();
				} else {
					$end_time = $val['send_sample_date'];
				}
				$sample_list[$key]['sample_made_days'] = intval(($end_time - $val['to_factory_date']) / 60 / 60 / 24) + 1;
			}

			$sample_list[$key]["pro_records"]=$pro_records_list=$mod_pro_records->find(array(
					'conditions'=>"key_id={$val['sample_id']} AND type=".PRO_RECORDS_SAMPLES,
					'fields'=>"rec_time,rec_buff",
					'limit'	=>"0,5",
					'order'	=>"rec_time desc"
							));
		}

		$page['item_count'] = $this->_sample_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);

		$this->assign('sample_list', $sample_list);
		$this->display('sample.approve.html');
		$this->_curlocal(Lang::get('sample_approve'));
	}

	//检查样板是否能被哪种角色编辑
	function _check_sample_is_edit($sample)
	{

		$role=$this->visitor->get('role');
		$merch_ids = explode(',', $this->visitor->get('merchandiser'));

		switch($sample['status'])
		{
			case SAMPLE_STATUS_CREATE:		//新建
				if (($role==USER_ASSISTANT&&!in_array($sample['merchandiser_id'], $merch_ids))||($role==USER_MERCHANDISER&&$sample['merchandiser_id'] != $this->visitor->get('user_id'))||!in_array($role,array(USER_ASSISTANT,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_MANAGER,USER_CIVILIAN,USER_MERCHANT))) {
					return false;
				}
				break;
			case SAMPLE_STATUS_APPROVE:		//主管审批
				if (($role==USER_ASSISTANT&&!in_array($sample['merchandiser_id'], $merch_ids))||($role==USER_MERCHANDISER&&$sample['merchandiser_id'] != $this->visitor->get('user_id'))||!in_array($role,array(USER_ASSISTANT,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_MANAGER,USER_CIVILIAN,USER_MERCHANT))) {
					return false;
				}
				break;
			case SAMPLE_STATUS_SEND_FACTORY:		//已送工厂
				if (($role==USER_ASSISTANT&&!in_array($sample['merchandiser_id'], $merch_ids))||($role==USER_MERCHANDISER&&$sample['merchandiser_id'] != $this->visitor->get('user_id'))||($role==USER_FACTORY && $sample['factory_id'] != $this->visitor->get('factory_id'))||!in_array($role,array(USER_ASSISTANT,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_MANAGER,USER_FACTORY,USER_CIVILIAN,USER_SAMPLE,USER_SOURCING,USER_FINANCE,USER_MERCHANT))) {
					return false;
				}
				break;
			case SAMPLE_STATUS_FACTORY_SUBMIT:		//提交样板
				if (($role==USER_ASSISTANT && !in_array($sample['merchandiser_id'], $merch_ids)) || ($role==USER_MERCHANDISER && $sample['merchandiser_id'] != $this->visitor->get('user_id')) ||!in_array($role,array(USER_ASSISTANT,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_MANAGER,USER_CIVILIAN,USER_MERCHANT)))
				{
					return false;
				}
				break;

			case SAMPLE_STATUS_SEND_SAMPLE:		//已寄板
			 if (($role==USER_ASSISTANT&&!in_array($sample['merchandiser_id'], $merch_ids))||($role==USER_MERCHANDISER&&$sample['merchandiser_id'] != $this->visitor->get('user_id'))||!in_array($role,array(USER_ASSISTANT,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_MANAGER,USER_CIVILIAN,USER_MERCHANT)))  {
			 	return false;
			 }
			 break;
			default:				//其他
				return false;
					
		}
		return true;
	}

	//ajax根据品牌获取买手
	function ajax_get_buyer()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$brand_mod =& m('brand');
		$brand_info=$brand_mod->get($id);
		$con=empty($id)?"company_id!={$id}":"company_id={$brand_info['company_id']} and {$id} in (brand_arr_id)";
		$buyer_mod=&m("member");
		$buyer_info=$buyer_mod->find(array(
				'conditions' => "{$con} AND if_show = 1 and activation=1",
				'order' => 'user_name ASC'));

		echo json_encode($buyer_info);
	}

	//ajax根据档口获取货号
	function ajax_get_fabric_shop(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$con = empty($id)?"fabric_shop_id!={$id}":"fabric_shop_id={$id}";
		$fabric_info = &m('fabric_info');
		$info = $fabric_info->find(array('fields'=>'item','conditions'=>"{$con}",'order'=>'item ASC'));
		foreach($info as $key=>$value){
			$info[$key] = $value[item];
		}
		$info_list = array_unique($info);
		echo json_encode($info_list);
	}


	function emailset()
	{
		return;
		$model_setting = &af('settings');
		$setting = $model_setting->getAll(); //载入系统设置数据
		if (!IS_POST)
		{
			$this->assign('setting', $setting);
			$this->assign('mail_type', array(
					MAIL_PROTOCOL_SMTP  => Lang::get('smtp'),
					MAIL_PROTOCOL_LOCAL => Lang::get('email'),
			));
			$this->display('sample.email_setting.html');
		}
		else
		{
			$data['email_type']     = $_POST['email_type'];
			$data['email_host']     = $_POST['email_host'];
			$data['email_port']     = $_POST['email_port'];
			$data['email_addr']     = $_POST['email_addr'];
			$data['email_id']       = $_POST['email_id'];
			$data['email_pass']     = $_POST['email_pass'];
			$data['email_test']     = $_POST['email_test'];
			$data['email_open']     = intval($_POST['email_open']);
			$model_setting->setAll($data);

			$this->show_message('edit_email_setting_successed');
		}
			
	}


	function send_test_email()
	{
		return;
		if (IS_POST)
		{
			$email_from = Conf::get('site_name');
			$email_type = $_POST['email_type'];
			$email_host = $_POST['email_host'];
			$email_port = $_POST['email_port'];
			$email_addr = $_POST['email_addr'];
			$email_id   = $_POST['email_id'];
			$email_pass = $_POST['email_pass'];
			$email_test = $_POST['email_test'];
			$email_subject = Lang::get('email_subjuect');
			$email_content = Lang::get('email_content');

			/* 使用mailer类 */
			import('mailer.lib');
			$mailer = new Mailer($email_from, $email_addr, $email_type, $email_host, $email_port, $email_id, $email_pass);
			$mail_result = $mailer->send($email_test, $email_subject, $email_content, CHARSET, 1);
			if ($mail_result)
			{
				$this->json_result('', 'mail_send_succeed');
			}
			else
			{
				$this->json_error('mail_send_failure', implode("\n", $mailer->errors));
			}
		}
		else
		{
			$this->show_warning('Hacking Attempt');
		}
	}

	function test()
	{
		return;
		$sample = $this->_sample_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sample_id = 41' ,
				'join' => 'belongs_brand,belongs_buyer,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
		));
		//print_r($sample);exit();
		/* 发送邮件 */
		$mail = get_mail('tofactory', array('sample' => $sample));
			
		$this->_mailto("252629505@qq.com", addslashes($mail['subject']), addslashes($mail['message']));
			
	}

	function _assign_next_sample($sample_id)
	{
		if(!empty($sample_id))
		{
			$con="sample_id >{$sample_id}";
			if($this->visitor->get('role')==USER_FACTORY)
			{
				$con.=" AND sample.factory_id={$this->visitor->get('factory_id')}";
			}
			$sample = $this->_sample_mod->get(array(
					'fields' => '*,sample.description',
					'conditions' =>$con,
					'join' => 'belongs_brand,belongs_merchandiser,belongs_style,belongs_special,belongs_factory',
			));

			if(!empty($sample))
			{
				$sample['is_edit']=$this->_check_sample_is_edit($sample);
				$this->assign("next_sample",$sample);
			}

		}
	}

	function approve_not_pass()
	{
	 $this->display('sample.form_approve_not_pass.html');
	}

	function return_factory()
	{
	 $this->display('sample.form_return_factory.html');
	}

	function clear_cache()
	{
		$cache_server =& cache_server();
		$cache_server->clear();
		$this->_session->_sess_gc();
		echo('clear_cache_ok');
	}

	function usortSample(&$sample_list,$order)
	{
		if(empty($sample_list)){
			return false;
		}
		$cmp_arr=array();
		foreach($sample_list as $item)
		{
			$cmp_arr[$item["sample_id"]]=$item["sample_made_days"];
		}

		if($order=="asc")
		{
			asort($cmp_arr);
		}
		elseif($order=="desc")
		{
			arsort($cmp_arr);
		}
		foreach($cmp_arr as $key=>$item)
		{
			$cmp_arr[$key]=$sample_list[$key];
		}
		$sample_list=$cmp_arr;
		return true;
	}
	
	
	//分派用户样板模块
	function batch_edit(){
		$user_info=$this->visitor->get();
		$member_mod=&m('member');
		$sample_id=isset($_GET['id'])?$_GET['id']:0;
		if (($user_info['role']==USER_MERCHANDISER && $user_info['role_cate']==ROLE_MERCH) || $user_info['role']==USER_MANAGER){
			if (!$sample_id){
				$this->show_warning('sample_not_exist');
				return;
			}
			if (!IS_POST){
				$conditions="activation=1 and role=".USER_CLIENT;
				$country_list=$member_mod->get_consumer_country($conditions,'mem.user_id');
				if (!empty($country_list)){
					foreach ($country_list as $key=>$rows){
						$countrys=$rows['country'];
						$country_info=$member_mod->find(array(
							'join'=>'has_consumer',
							'fields'=>'user_name,member.user_id',
							'conditions'=>"activation=1 and role=".USER_CLIENT." and country='".$countrys."'"	
						));
						$country_list[$key]['user_list']=$country_info;
					}
				}
				$this->assign('country_list',$country_list);
				$this->display('sample.batch.html');
			}else{
				$data=array();
				$consumer_arr_id=isset($_POST['consumer_arr_id'])?$_POST['consumer_arr_id']:0;
				if (empty($consumer_arr_id)){
					$this->show_message('没有数据提交');
					exit;
				}
				$consumer_arr=implode(',', $consumer_arr_id);
				$data['consumer_arr_id']=$consumer_arr;
				$this->_sample_mod->edit("sample_id in ($sample_id)",$data);
				if ($this->_sample_mod->has_error()){
					$this->show_warning($this->_sample_mod->get_error());
					return;
				}
				$this->show_message('分派客户模板成功');
			}
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	//取消全部的模板分配
	function batch_cancel(){
		$user_info=$this->visitor->get();
		if (($user_info['role']==USER_MERCHANDISER && $user_info['role_cate']==ROLE_MERCH) || $user_info['role']==USER_MANAGER){
			//$merch_id=$user_info['user_id'];
			$this->_sample_mod->edit('1=1',array('consumer_arr_id'=>null));
			if ($this->_sample_mod->has_error()){
				$this->show_warning($this->_sample_mod->get_error());
				return;
			}
			$this->show_message('取消全部分派客户模板成功');
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	//取消全部的展示样板
	function reveal_cancel(){
		$user_info=$this->visitor->get();
		if (($user_info['role']==USER_MERCHANDISER && $user_info['role_cate']==ROLE_MERCH) || $user_info['role']==USER_MANAGER){
			//$merch_id=$user_info['user_id'];
			$this->_sample_mod->edit('is_preset=1',array('is_preset'=>0));
			if ($this->_sample_mod->has_error()){
				$this->show_warning($this->_sample_mod->get_error());
				return;
			}
			$this->show_message('取消全部默认展示样板成功');
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	//上传面料附件
	function upload_fabric(){
		$f_id=empty($_GET['fid'])?0:intval($_GET['fid']);
		$user_info=$this->visitor->get();
		if (!$f_id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$this->assign('user',$user_info);
		$fabric_mod=&m('fabric_info');
		$fabric_info=$fabric_mod->get(array(
				'conditions'=>'fabric_info_id='.$f_id
		));
		if (empty($fabric_info)){
			$this->show_warning('fabric_not_exist');
			return;
		}
		$file_mod=&m('upload');
		$fabric_info['sample_fabric'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_FABRIC_INFO . ' AND key_id = ' . $f_id,
				'order' => 'file_id ASC',
		));
		$this->assign('belongs_fabric_info',BELONGS_FABRIC_INFO);
		$this->assign('fabric_info',$fabric_info);
		$this->display('fabric.upload.html');
	}
	
	
	//系统分配展示模板给用户
	function assign_sys_sample(){
		$user_info=$this->visitor->get();
		$sample_id=isset($_GET['id'])?$_GET['id']:0;
		if (($user_info['role']==USER_MERCHANDISER && $user_info['role_cate']==ROLE_MERCH) || $user_info['role']==USER_MANAGER){
			if (!$sample_id){
				$this->show_warning('sample_not_exist');
				return;
			}
			if (!IS_POST){
				$this->display('sample.sys_assign.html');
			}else{
				$data=array();
				$is_preset=isset($_POST['is_preset'])?$_POST['is_preset']:0;
				$data['is_preset']=$is_preset;
				$this->_sample_mod->edit("sample_id in ($sample_id)",$data);
				if ($this->_sample_mod->has_error()){
					$this->show_warning($this->_sample_mod->get_error());
					return;
				}
				$this->show_message('分派系统展示模板成功');
			}
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	//设为默认图片
	function default_image(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$sample_id = empty($_GET['sample_id']) ? 0 : intval($_GET['sample_id']);
		$result=array();
		if (!$id && !$sample_id) {
			if ($_GET['ajax']) {
				$result=array('done'=>'false','msg'=>Lang::get('file_not_exist'));
				exit(ecm_json_encode($result));
			}
			return;
		}
	
		$upload_mod =& m('upload');
		$file = $upload_mod->get_info($id);
		if (empty($file)) {
			if ($_GET['ajax']) {
				$result=array('done'=>'false','msg'=>Lang::get('file_not_exist'));
				exit(ecm_json_encode($result));
			}
			return;
		}
	
		if (!$this->_sample_mod->edit($sample_id,array('default_image'=>$file['file_path']))) {
			if ($_GET['ajax']) {
				$result=array('done'=>'false','msg'=>Lang::get('setting_error'));
				exit(ecm_json_encode($result));
			}
			return;
		}
		if ($_GET['ajax']) {
			$result=array('done'=>'true','msg'=>Lang::get('setting_success'));
		}
		exit(ecm_json_encode($result));
	}
	
	
	//订单展示
	function order_reveal(){
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'style_id',
						'equal' => '=',
						'name'	 => 'style_id'
				)
		));
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		$style_mod =& m('style');
		$order_mod=&m('order');
		$member_mod=&m('member');
		$member_info=$member_mod->get($user_info['user_id']);
		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show = 1', 'order'=>'style_name ASC')));
		if ($user_info['role']==USER_CLIENT){
			if ($user_info['is_vip']==1){
				$conditions.=" and brand_id not ".db_create_in(array($member_info['brand_arr_id'])).' and default_image is not null';
			}else{
				$conditions .=" and (is_preset=1 or FIND_IN_SET({$user_info['user_id']},consumer_arr_id)) and default_image is not null";
			}
			$order_list = $order_mod->find(array(
					'conditions' => 'status != ' . ORDER_STATUS_NO_CANCEL . $conditions,
					'count' => true,
					'limit' => $page['limit']
			));
			$file_mod=&m('upload');
			if (!empty($order_list)){
				foreach ($order_list as $key=>$rows){
					$buyer_id=$rows['buyer_id'];
					if ($buyer_id==$user_info['user_id']){
						unset($order_list[$key]);
					}
					$order_thumb = $file_mod->find(array(
							'conditions' => 'file_type = ' . BELONGS_ORDER_THUMB_IMG . ' AND key_id = ' . $rows['order_id'],
							'order' => 'file_id ASC',
					));
					if (empty($order_thumb)){
						unset($order_list[$key]);
					}
				}
			}
		}
		$page['item_count'] = $order_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('order_list', $order_list);
		$this->_curlocal(Lang::get('order_reveal'));
		$this->display('sample.order_reveal.html');
	}
	
	
	//订单详细
	function order_detail(){
		$order_id=empty($_GET['id'])?0:intval($_GET['id']);
		$user_info=$this->visitor->get();
		$order_mod=&m('order');
		if (!$order_id){
			$this->show_warning('order_not_exist');
			return;
		}
		$member_mod=&m('member');
		$member_info=$member_mod->get($user_info['user_id']);
		if ($user_info['is_vip']==1){
			$conditions.=" and brand_id not ".db_create_in(array($member_info['brand_arr_id'])).' and order_id='.$order_id.' and default_image is not null';
		}else{
			$conditions .=" and (is_preset=1 or FIND_IN_SET({$user_info['user_id']},consumer_arr_id)) and order_id='.$order_id.' and default_image is not null";
		}
		$order_info=$order_mod->get(array(
				'conditions'=>"1=1".$conditions,
				'join'=>'belongs_style'
		));
		if (!$order_info){
			$this->show_warning('order_not_exist');
			return;
		}
		//细节图
		$file_mod=& m('upload');
		$order_info['consumer_order_thumb'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_ORDER_THUMB_IMG . ' AND key_id = ' . $order_id,
				'order' => 'file_id ASC',
		));
		$this->assign('order',$order_info);
		$this->_curlocal(Lang::get('order_reveal'),'index.php?app=sample&act=order_reveal',Lang::get('detail_order'));
		$this->display('sample.order_detail.html');
	}
	
	
	function cancel_user_assign(){
		$uid=isset($_GET['uid'])?$_GET['uid']:0;
		$id=isset($_GET['id'])?$_GET['id']:'';
		if(!$id && !$uid){
			$this->show_warning("参数不对");
			return;
		}
		$ids=explode(',', $id);
		$sample_list=$this->_sample_mod->find(array(
				'conditions'=>'sample_id'.db_create_in($ids),
				'fields'=>'consumer_arr_id,buyer_id'
		));
		if(!empty($sample_list)){
			foreach($sample_list as $key=>$val){
				$consumer_arr_id=explode(',', $val['consumer_arr_id']);
				if (in_array($uid, $consumer_arr_id)){
					unset($consumer_arr_id[array_search($uid , $consumer_arr_id)]);
				}
				$this->_sample_mod->edit($val['sample_id'],array('consumer_arr_id'=>implode(',', $consumer_arr_id)));
				if ($this->_sample_mod->has_error()){
					$this->show_warning($this->_sample_mod->get_error());
					return;
				}
			}
		}
		$this->show_message("取消用户展示成功");
	}
	
	
	//上传细节图
	function upload_thumb(){
		$sample_id=isset($_GET['sample_id'])?intval($_GET['sample_id']):0;
		if (!$sample_id){
			$this->show_warning("样板不存在");
			return;
		}
		$user_info=$this->visitor->get();
		if(in_array($user_info['role'], array(USER_CLIENT,USER_FACTORY))){
			$this->show_warning('no_powers_to_visit');
			return;
		}else{
			$sample=$this->_sample_mod->get($sample_id);
			if (empty($sample)){
				$this->show_warning("样板不存在");
				return;
			}
			//细节图
			$file_mod=&m('upload');
			$sample['consumer_sample_thumb'] = $file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_SAMPLE_THUMB_IMG . ' AND key_id = ' . $sample_id,
					'order' => 'file_id ASC',
			));
			$this->assign('belongs_sample_thumb',BELONGS_SAMPLE_THUMB_IMG);
			$this->assign('sample',$sample);
			$this->assign('user',$user_info);
			$this->display('sample.thumb.html');
		}
	}
	
	
	//直接归档样板
	function file_stock(){
		$user_info=$this->visitor->get();
		$id=!empty($_GET['id'])?$_GET['id']:0;
		if(!$id){
			$this->show_warning("参数不对");
			return;
		}
		$ids=explode(',', $id);
		if ($user_info['role']==USER_CIVILIAN){
			//$merch_id=$user_info['user_id'];
			$this->_sample_mod->edit($ids,array('status'=>SAMPLE_STATUS_STOCK));
			if ($this->_sample_mod->has_error()){
				$this->show_warning($this->_sample_mod->get_error());
				return;
			}
			$this->show_message('样板归档成功');
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
}
?>
